Search code examples
c++winapiencoding

Using itoa with a TCHAR


I have a Win32 C++ Application 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 );

Solution

  • 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::to_wstring function from the <string> header; it's just as convenient as itoa and more safe.