Jump to content
  • Advertisement
Sign in to follow this  
maxest

Conversion Between std::string and std::wstring

This topic is 793 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

Advertisement

No, you are doing it wrong. While your code appears to be working, it will only do so when there is a guarantee the string only contains ISO-8859-1 characters. Directory names can contain almost any character (even outside the range of what a wchar_t might be able to represent). You are also mistaken in the += operator of strings/wstrings automatically convert the characters, you are just casting them before handing them to it. 

 

I don't blame you, this stuff is hard. You need to keep track of the encoding of each string and properly convert between them. A simple cast is not enough. The cast from char to wchar_t is fine (if ISO-8859-1), but the cast from wchar_t to char will throw away the upper bits of the character, potentially garbling your text.

 

Have a look at mbsrtowcs/wcsrtombs. In C++11 they also added codecvt to convert between encodings.

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!