C++ 中的字符串编码类型转换

参考博客: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](https://zh.cppreference.com/w/cpp/string/multibyte/mbrtoc16) / [c16rtomb](https://zh.cppreference.com/w/cpp/string/multibyte/c16rtomb)(有 C11 的 DR488) [codecvt](https://zh.cppreference.com/w/cpp/locale/codecvt)

[codecvt_utf8_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8_utf16)

[codecvt_utf8_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8_utf16)

[codecvt_utf8_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8_utf16)
N/A
UCS2 [c16rtomb](https://zh.cppreference.com/w/cpp/string/multibyte/c16rtomb)(无 C11 的 DR488) [codecvt_utf8](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8)


[codecvt_utf8](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8)(Windows)
[codecvt_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf16)


[codecvt_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf16)(Windows)
UTF-32 [mbrtoc32](https://zh.cppreference.com/w/cpp/string/multibyte/mbrtoc32) / [c32rtomb](https://zh.cppreference.com/w/cpp/string/multibyte/c32rtomb) [codecvt](https://zh.cppreference.com/w/cpp/locale/codecvt)

[codecvt_utf8](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8)

[codecvt_utf8](https://zh.cppreference.com/w/cpp/locale/codecvt_utf8)(非 Windows)
[codecvt_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf16)

[codecvt_utf16](https://zh.cppreference.com/w/cpp/locale/codecvt_utf16)(非 Windows)
系统宽

UTF-32(非 Windows)

UCS2(Windows)
[mbsrtowcs](https://zh.cppreference.com/w/cpp/string/multibyte/mbsrtowcs) / [wcsrtombs](https://zh.cppreference.com/w/cpp/string/multibyte/wcsrtombs)

[use_facet](https://zh.cppreference.com/w/cpp/locale/use_facet)<[codecvt](https://zh.cppreference.com/w/cpp/locale/codecvt)

>([locale](https://zh.cppreference.com/w/cpp/locale/locale))

整理好的转换函数:

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); }

原文链接: https://www.cnblogs.com/Asp1rant/p/12299838.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/193047

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午6:14
下一篇 2023年2月12日 下午6:15

相关推荐