Jump to content
  • Advertisement
Sign in to follow this  

Conversion Between std::string and std::wstring

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


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!