Should I use wchar_t when using UTF-8?(使用 UTF-8 时应该使用 wchar_t 吗?)
问题描述
UTF-8 可以编码为 1、2 和最多 4 个字节.我的系统上的单个 char 是 1 个字节.我是否应该使用 wchar_t 作为预防措施,以便我能够适应任何任意的 UTF-8 编码字符?
UTF-8 can encode in 1, 2, and up to 4 bytes. A single char on my system is 1 byte. Should I use wchar_t as a precaution so that I will be able to fit any arbitrary UTF-8 encoded character?
推荐答案
不,你不应该!Unicode 4.0 标准 (ISO 10646:2003) 指出:
No, you should not! The Unicode 4.0 standard (ISO 10646:2003) notes that:
wchar_t 的宽度是特定于编译器的,可以小到 8 位.因此,需要跨任何 C 或 C++ 编译器移植的程序不应使用 wchar_t 来存储 Unicode 文本.
The width of
wchar_tis compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not usewchar_tfor storing Unicode text.
在大多数情况下,UTF-8 文本的字符性质"与您的程序无关,因此将其视为 char 元素的数组,就像任何其他字符串一样,将是充足的.但是,如果您需要提取单个字符,这些字符应以至少 24 位宽的类型存储(例如,uint32_t),以便容纳所有 Unicode 代码点.
Under most circumstances, the "character nature" of UTF-8 text will not be relevant to your program, so treating it as an array of char elements, just like any other string, will be sufficient. If you need to extract individual characters, though, those characters should be stored in a type that is at least 24 bits wide (e.g, uint32_t), in order to accomodate all Unicode code points.
这篇关于使用 UTF-8 时应该使用 wchar_t 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UTF-8 时应该使用 wchar_t 吗?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
