[cfe-dev] Convert char to wchar_t?

Aaron Wishnick aaron.s.wishnick at gmail.com
Sat May 26 13:41:57 PDT 2012


Thank you for your reply. I should have been more clear -- I am writing code in clang to do this. I can't use the local of the machine clang is running on, because I need to respect the target architecture, not just in its encoding of wchar_t, but its endianness.

Thanks!
Aaron

On May 26, 2012, at 4:32 PM, Suman Kar wrote:

> 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