_itow_s problems

Started by
5 comments, last by LittleJimmy 15 years, 10 months ago
Hey, I'm attempting to convert from an int to a wchar_t like so:


int num = 55;
wchar_t *wchar = new wchar_t[3];

_itow_s(num, wchar, 3, 10);

std::cout << *wchar;


the code compiles fine but when i run it, it outputs the number 53 instead of 55, howcome? thanks
Advertisement
53 is the ASCII value for '5'. Because you're sending a Unicode character to an non-Unicode stream it actually gets treated as an int. You're also trying to print a single character rather than the whole string.

You'll get what you expect if you change it to 'std::wcout << wchar;'. You might also consider using wstringstream or boost::lexical_cast instead of _itow_s.

-Mike
Because you're printing out the character code of the first L'5'.
int num = 55;std::wstring string = boost::lexical_cast< std::wstring >(num);std::wcout << string;
Σnigma
int num = 55;wchar_t *wchar = new wchar_t[3];_itow_s(num, wchar, 3, 10);std::wcout << wchar;


Use std::wcout with wide characters, and you don't have to dereference C strings to print them out (just make sure they are correctly null terminated).

But, really, you should be using a std::wstring and std::wstringstream.
oh okay cool.

Hmmm... I've never seen the boost namespace before.

What is it exactly and which header do I include to use it?

thanks
Quote:Original post by LittleJimmy
Hmmm... I've never seen the boost namespace before.

What is it exactly and which header do I include to use it?


Boost
Huzzah! :)

Okay thanks for the help guys :)

This topic is closed to new replies.

Advertisement