标签:convert enc tle c++ 字节 定义 details har class
参考博客:https://blog.csdn.net/qq_41015048/article/details/102558047
std::wstring_convert
template < class Codecvt, class Elem = wchar_t, class Wide_alloc = std::allocator<Elem>, class Byte_alloc = std::allocator<char> > class wstring_convert;
实现string类型和wstring类型之间的转换,需要传递codecvt作为模板参数
std::codecvt
参数一览
字符转换 | 本地环境定义多字节 (UTF-8, GB18030) | UTF-8 | UTF-16 |
---|---|---|---|
UTF-16 | mbrtoc16 / c16rtomb(有 C11 的 DR488) | codecvt<char16_t, char, mbstate_t> codecvt_utf8_utf16<char16_t> codecvt_utf8_utf16<char32_t> codecvt_utf8_utf16<wchar_t> |
N/A |
UCS2 | c16rtomb(无 C11 的 DR488) | codecvt_utf8<char16_t> codecvt_utf8<wchar_t>(Windows) |
codecvt_utf16<char16_t> codecvt_utf16<wchar_t>(Windows) |
UTF-32 |
codecvt<char32_t, char, mbstate_t> |
codecvt_utf16<char32_t> |
|
系统宽 UTF-32(非 Windows) UCS2(Windows) |
mbsrtowcs / wcsrtombs |
无 | 无 |
整理好的转换函数:
1 std::u16string to_utf16( std::string str ) 2 { return std::wstring_convert< std::codecvt_utf8_utf16<char16_t>, char16_t >{}.from_bytes(str); } 3 4 std::string to_utf8( std::u16string str16 ) 5 { return std::wstring_convert< std::codecvt_utf8_utf16<char16_t>, char16_t >{}.to_bytes(str16); } 6 7 std::u32string to_utf32( std::string str ) 8 { return std::wstring_convert< std::codecvt_utf8<char32_t>, char32_t >{}.from_bytes(str); } 9 10 std::string to_utf8( std::u32string str32 ) 11 { return std::wstring_convert< std::codecvt_utf8<char32_t>, char32_t >{}.to_bytes(str32); } 12 13 std::wstring to_wchar_t( std::string str ) 14 { return std::wstring_convert< std::codecvt_utf8<wchar_t>, wchar_t >{}.from_bytes(str); } 15 16 std::string to_utf8( std::wstring wstr ) 17 { return std::wstring_convert< std::codecvt_utf8<wchar_t>, wchar_t >{}.to_bytes(wstr); }
标签:convert enc tle c++ 字节 定义 details har class
原文地址:https://www.cnblogs.com/Asp1rant/p/12299838.html