Sign in to follow this  

_itow_s problems

This topic is 3483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This topic is 3483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this