c++ - C++0x 是否支持 std::wstring 与 UTF-8 字节序列的转换?

标签 c++ c++11 unicode utf-8 wstring

我看到 C++0x 将添加对 UTF-8、UTF-16 和 UTF-32 文字的支持。但是三种表示之间的转换呢?

我计划在我的代码中到处使用 std::wstring。但是在处理文件和网络时,我还需要操作 UTF-8 编码的数据。 C++0x 是否也会提供对这些操作的支持?

最佳答案

在 C++0x 中,char16_tchar32_t将用于存储 UTF-16 和 UTF-32 而不是 wchar_t .

来自 n2798 草案:

22.2.1.4 Class template codecvt

2 The class codecvt is for use when converting from one codeset to another, such as from wide characters to multibyte characters or between wide character encodings such as Unicode and EUC.

3 The specializations required in Table 76 (22.1.1.1.1) convert the implementation- defined native character set. codecvt implements a degenerate conversion; it does not convert at all. The specialization codecvt<char16_t, char, mbstate_t> converts between the UTF-16 and UTF-8 encodings schemes, and the specialization codecvt <char32_t, char, mbstate_t> converts between the UTF-32 and UTF-8 encodings schemes. codecvt<wchar_t,char,mbstate_t> converts between the native character sets for narrow and wide characters. Specializations on mbstate_t perform conversion between encodings known to the library implementor.

Other encodings can be converted by specializing on a user-defined stateT type. The stateT object can contain any state that is useful to communicate to or from the specialized do_in or do_out members.

关于 wchar_t事情是它不能为您提供有关所使用编码的任何保证。它是一种可以容纳多字节字符的类型。时期。如果您现在要编写软件,您就必须接受这种妥协。 C++0x 兼容的编译器还相去甚远。您可以随时试用 VC2010 CTP 和 g++ 编译器,以了解它的值(value)。此外,wchar_t在不同的平台上有不同的大小,这是另一件需要注意的事情(VS/Windows 上 2 个字节,GCC/Mac 上 4 个字节等等)。然后有像-fshort-wchar这样的选项海湾合作委员会使问题进一步复杂化。

因此,最好的解决方案是使用现有的库。追逐 UNICODE 错误并不是对精力/时间的最佳利用。我建议您看一下:

有关 C++0x Unicode 字符串文字的更多信息 here

关于c++ - C++0x 是否支持 std::wstring 与 UTF-8 字节序列的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/621635/

相关文章:

c++ - 当被调用方法使用调用者已经锁定的相同锁时,如何避免死锁?

c++ - 共享互斥量是否比相对较大结构的原子更有效?

c++ - 警告 : non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

unicode - 在 Windows XP 中搜索 unicode 文本

c++ - 我可以使用 break 退出多个嵌套的 'for' 循环吗?

c++ - C++ 中的函数和函数指针

c++ - std::max 在统一初始化和 constexpr int 下表现出乎意料

c++ - 在两个范围内按降序对 vector 进行排序

java - 使用java从unicode解码字符

iphone - 将 NSString 与 UTF32 相互转换