Jump to content
  • Advertisement
Sign in to follow this  
LittleJimmy

_itow_s problems

This topic is 3737 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
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.

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
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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!