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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, in
<cstdlib>you havembstowcs()andwcstombs().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>orstd::vector<wchar_t>, coming to think of it.Note that
wchar_thas nothing to do with Unicode. If you need Unicode, you need to further convert fromwchar_tto Unicode using a separate library (likeiconv()), and don’t usewchar_tas the data type for Unicode codepoints. Instead, useuint32_ton legacy systems orchar32_ton modern ones.