Using itoa with a TCHAR
I have a Win32 C++ A开发者_高级运维pplication which supports UNICODE. I have a problem where I am using the itoa() function but I get a compile error because I am passing a TCHAR* as the parameter instead of a char*.
What can I do to make it work?
TCHAR buf[32];
itoa( taskState, buf, 10 );
For TCHAR strings you should use _ttoi().
There is no good reason to us TCHAR
unless you're targeting Windows 9x with MFC in DLL, and you're sort of afraid to rebuild MFC. Just use wchar_t
.
Then read Microsoft's documentation of itoa
.
There you find at least one variant that you can use with a wchar_t
string.
It's generally not a good idea to use itoa
family, but you're at the stage where you have not yet learned to look up the documentation.
So, that's what you should do: check the documentation (and yes, I have checked, and it is there).
Addendum, as of April 2015: Since mid 2011 we've had the C++11 and C++14 standards ratified, and compilers now commonly support the C++11 std::towstring
function from the <string>
header; it's just as convenient as itoa
and more safe.
As Adam Pierce mentioned, the TCHAR version of _itoa() is _itot(), you will find it here: http://msdn.microsoft.com/en-us/library/yakksftt(v=vs.80).aspx
精彩评论