C++ 如何将 DWORD 转换为 char *?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9272415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert DWORD to char *?
提问by psp
I am trying something like this,
我正在尝试这样的事情,
PROCESS_INFORMATION processInfo = .....
strcat( args, processInfo.dwProcessId);
where args
is a char *
which I need to pass as an argument to another executable.
其中args
是char *
该我需要作为参数传递到另一个可执行。
回答by Ajit Vaze
You can use sprintf
您可以使用 sprintf
char procID[10];
sprintf(procID, "%d", processInfo.dwProcessId);
This will convert the processInfo.dwProcessId into a character which can then be used by you.
这会将 processInfo.dwProcessId 转换为您可以使用的字符。
回答by Java
回答by Alexey Frunze
MSDN has pretty good documentation, check out the Data Conversion page.
MSDN 有很好的文档,查看数据转换页面。
There's sprintf()too.
还有sprintf()。
回答by André Caron
While not directly "converting to a char*
", the following should do the trick:
虽然不是直接“转换为 a char*
”,但以下应该可以解决问题:
std::ostringstream stream;
stream << processInfo.dwProcessId;
std::string args = stream.str();
// Then, if you need a 'const char*' to pass to another Win32
// API call, you can access the data using:
const char * foo = args.c_str();