How can I convert from ANSI character (char) to Unicode character (wchar_t) and vice versa?

StackOverflow https://stackoverflow.com/questions/7333256

  •  27-10-2019
  •  | 
  •  

Question

How can I convert from ANSI character (char) to Unicode character (wchar_t) and vice versa?

Is there any cross-platform source code for this purpose?

Was it helpful?

Solution

Yes, in <cstdlib> you have mbstowcs() and wcstombs().

I've previously posted some code on how to use this, maybe that's helpful. Make sure you run the function twice, once to get the length and once to do the actual conversion. (Here's a little discussion of what the functions mean.) Instead of the manual char array, I would probably prefer a std::vector<char> or std::vector<wchar_t>, coming to think of it.

Note that wchar_t has nothing to do with Unicode. If you need Unicode, you need to further convert from wchar_t to Unicode using a separate library (like iconv()), and don't use wchar_t as the data type for Unicode codepoints. Instead, use uint32_t on legacy systems or char32_t on modern ones.

OTHER TIPS

Apparently this works, I don't know if it will always work or if it's a coincidence, but I thought it was worth showing:

const char* c = "hey yo";
wstring s(c, c + 6);

wcout << s << endl;
wcin.get();

prints

hey yo

Look at libraries like ICU and iconv if you really are using Unicode and not just 16 bit characters. That is Unicode does not just deal with single characters not even 16 bit ones as plain wchar_t does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top