gpt4 book ai didi

c - C 如何处理包含空字符的多字节字符?

转载 作者:行者123 更新时间:2023-12-04 10:49:26 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is a multibyte character set?

(9 个回答)


去年关闭。




C11 标准的第 5.2.1.2.1 节规定:

A byte with all bits zero shall be interpreted as a null character independent of shift state. Such a byte shall not occur as part of any other multibyte character.



我认为,这清楚地表明多字节字符的任何字节都不能为零。如果是这样,则下面的示例将不起作用(因为字符包含零字节);但确实如此。
char16_t wc= u'\u1200';
char16_t wcs[] = u'\u1200\u1300';
printf("%#x, %#x, %#x\n", wc, wcs[0], wcs[1]);

为什么?我在这里缺少什么?

最佳答案

我认为您误解了术语多字节字符(这可能是一个模棱两可的术语)。例如,来自 this page :

The term “multibyte character” is defined by ISO C to denote a byte sequence that encodes an ideogram, no matter what encoding scheme is employed. All multibyte characters are members of the “extended character set.” A regular single-byte character is just a special case of a multibyte character. The only requirement placed on the encoding is that no multibyte character can use a null character as part of its encoding.



因此,您引用的条件是指作为 数组的字符串。单字节 元素,但可以包含需要多个单字符元素来表示的字符。
char16_t您使用的类型是 宽字符 ,其中所有可表示的字符都被编码为 2 字节值 - 即使是像 ASCII 这样“简单”的东西'A',即 0x0041 .

编辑:我意识到我上面写的内容令人困惑!然而,在寻找一些澄清的基础时,我遇到了这篇 Stack Overflow 帖子: What is a multibyte character set?我无法真正改进那里给出的答案,所以也许它可以用作“重复”。

关于c - C 如何处理包含空字符的多字节字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com