简体   繁体   中英

c++ how to convert wchar_t to char16_t

Convert between string, u16string & u32string This post explains the opposite of my question. So I need to post a new question

I need to convert wchar_t to char16_t. I found a sample of doing the opposite ( char16_t -> wchar_t) here:

I am not familiar with templates etc, sorry. Can anybody give me an example of converting wchar_t to char16_t please?

I have this piece of code that I want to adapt for converting wchar_t to char16_t.

 std::wstring u16fmt(const char16_t* str) {
 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert_wstring;
 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
 std::string utf8str = convert.to_bytes(str);
 std::wstring wstr = convert_wstring.from_bytes(utf8str);
 return wstr;
}

Ah, and it should run on Windows and Linux

If sizeof( wchar_t ) == 2 (*), you're straddled with Windows and can only hope your wstring holds UTF-16 (and hasn't been smashed flat to UCS-2 by some old Windows function).

If sizeof( wchar_t ) == 4 (*), you're not on Windows and need to do a UTF-32 to UTF-16 conversion.

(*): Assuming CHAR_BIT == 8 .

I am, however, rather pessimistic about standard library's Unicode capabilities beyond simple "piping through", so if you're going to do any actual work on those strings, I'd recommend ICU , the de-facto C/C++ standard library for all things Unicode.

icu::UnicodeString has a wchar_t * constructor , and you can call getTerminatedBuffer() to get a (non-owning) const char16_t * . Or, of course, just use icu::UnicodeString , which uses UTF-16 internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM