使用 libstdc++ 在 string、u16string 和 u32string 之间进行转换

Convert between string, u16string & u32string using libstdc++

本文关键字:之间 u32string 转换 u16string libstdc++ string 使用      更新时间:2023-10-16

我发现了这个主题在字符串、u16string&u32string和该解决方案(看起来非常棒(只使用libc++而不使用libstdc++。目前libc++不可用-它很难编译,在Windows上也不太好用。

是否有任何方法可以使用C++11和在所有平台上都可用的libstdc++在这些表示之间进行转换?

我特别感兴趣的是将u32_string转换为字符串(utf8(,反之亦然。

C++11中有一种可移植的方法,可以通过wstring_convert类来实现这一点。

然而,它似乎还没有实现libstdc++(截至gcc 4.8(

同样适用于:

codecvt<char16_t, char, mbstate_t>.
codecvt<char32_t, char, mbstate_t>.
codecvt_utf8.
codecvt_utf16.
codecvt_utf8_utf16.

事实上,报头<codecvt>仍然不存在于gcc分布中。

您可以使用utf8cpp(http://utfcpp.sourceforge.net/)它将这些转换器提供为易于使用的C++。