Reputation: 13729
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?
Upvotes: 2
Views: 12575
Reputation: 476940
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.
Upvotes: 6
Reputation: 75130
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
Upvotes: 1