我看到 C++0x 将添加对 UTF-8、UTF-16 和 UTF-32 文字的支持。但是三种表示之间的转换呢?
我计划在我的代码中到处使用 std::wstring。但是在处理文件和网络时,我还需要操作 UTF-8 编码的数据。 C++0x 是否也会提供对这些操作的支持?
最佳答案
在 C++0x 中,char16_t
和 char32_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 specializationcodecvt <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 onmbstate_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/