[cfe-dev] Convert char to wchar_t?

Suman Kar skarpio at gmail.com
Sat May 26 13:32:28 PDT 2012


On Sun, May 27, 2012 at 1:37 AM, Aaron Wishnick
<aaron.s.wishnick at gmail.com> wrote:
> [...] question is, is there an example of existing code for taking a string,
> encoded as char, and turning it into an array of bytes appropriately encoded
> as wchar_t for the target platform?

Take a look here:
<http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/fa6d0fb5-949f-4f31-973d-ea112a4eec7f>

> If not, could anybody point me to a safe
> way to do this? Thanks!
>

The general idea is to set the locale (i.e. call `setlocale()`) and
then perform a multibyte to widechar translation. Since `wchar_t` does
not specify an encoding (i.e. the encoding varies from one platform to
another) you will need to make sure that your translation correctly
converts the input string to the desired *native* encoding. Little
endian UTF-16 is the encoding of choice on Windows platforms.

C++11 has `codecvt_facet` which may interest you.

Regards,
Suman



More information about the cfe-dev mailing list