I need a code in C++ to convert a string given in wchar_t* to a UTF-16 string. It must work both on Windows and Linux. I've looked through a lot of web-pages during the search, but the subject still is not clear to me.
As I understand I need to:
- Call
setlocalewith LC_TYPE and UTF-16 encoding. - Use
wcstombsto convertwchar_tto UTF-16 string. - Call
setlocaleto restore previous locale.
Do you know the way I can convert wchar_t* to UTF-16 in a portable way (Windows and Linux)?
wchar_tstring in? What type do you expect to use to represent the character type in the UTF-16 string? Is this simply a transform between UTF-32 (in thewchar_t) and UTF-16 inuint16_t? Or are you dealing with codeset conversion too? Portability is a noble goal; it is not always achievable, sadly. Do investigate ICU.