Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert wchar_t* to UTF-16 string

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:

  1. Call setlocale with LC_TYPE and UTF-16 encoding.
  2. Use wcstombs to convert wchar_t to UTF-16 string.
  3. Call setlocale to restore previous locale.

Do you know the way I can convert wchar_t* to UTF-16 in a portable way (Windows and Linux)?

like image 906
Andrei Baskakov Avatar asked Mar 14 '12 06:03

Andrei Baskakov


2 Answers

There is no single cross-platform method for doing this in C++03 (not without a library). This is in part because wchar_t is itself not the same thing across platforms. Under Windows, wchar_t is a 16-bit value, while on other platforms it is often a 32-bit value. So you would need two different codepaths to do it.

like image 155
Nicol Bolas Avatar answered Sep 18 '22 00:09

Nicol Bolas


C++11's std::codecvt_utf16 should work, I think.

std::codecvt_utf16 is a std::codecvt facet which encapsulates conversion between a UTF-16 encoded byte string and UCS2 or UCS4 character string (depending on the type of Elem).

See this: http://en.cppreference.com/w/cpp/locale/codecvt_utf16

like image 38
Pubby Avatar answered Sep 21 '22 00:09

Pubby