unicode - 将 UTF-16 视为固定的 16 位编码会带来什么问题?

标签 unicode utf-16 unicode-string

我正在阅读有关 Unicode 的一些问题,并且有一些我不完全理解的评论,例如:

Dean Harding: UTF-8 is a variable-length encoding, which is more complex to process than a fixed-length encoding. Also, see my comments on Gumbo's answer: basically, combining characters exist in all encodings (UTF-8, UTF-16 & UTF-32) and they require special handling. You can use the same special handling that you use for combining characters to also handle surrogate pairs in UTF-16, so for the most part you can ignore surrogates and treat UTF-16 just like a fixed encoding.



我对最后一部分(“大部分”)感到有些困惑。如果将 UTF-16 视为固定的 16 位编码,这会导致什么问题?在 BMP 之外存在字符的可能性有多大?如果有,如果您假设两个字节的字符,这会导致什么问题?

我在 Surrogates 上阅读了维基百科信息但它并没有真正让我更清楚!

编辑:我想我真正的意思是“当 UTF-16 看起来是假的时,为什么有人建议将其视为固定编码?”

编辑2:

我在“Is there any reason to prefer UTF-16 over UTF-8? ”中发现了另一条评论,我认为这更好地解释了这一点:

Andrew Russell: For performance: UTF-8 is much harder to decode than UTF-16. In UTF-16 characters are either a Basic Multilingual Plane character (2 bytes) or a Surrogate Pair (4 bytes). UTF-8 characters can be anywhere between 1 and 4 bytes



这表明所提出的观点是 UTF-16 不会有任何三字节字符,因此通过假设 16 位,您不会因为结束一字节而“完全搞砸”。但我仍然不相信这与假设 UTF-8 是单字节字符有什么不同!

最佳答案

重要的是要了解,即使是 UTF-32 也是固定长度的代码点,而不是字符。有许多字符由多个代码点组成,因此您无法真正拥有一个数字(代码单元)对应一个字符(如用户所感知)的 Unicode 编码。

要回答您的问题 - 将 UTF-16 视为固定长度编码形式的最明显问题是在代理对中间断开一个字符串,因此您会得到两个无效的代码点。这完全取决于您对文本的处理方式。

关于unicode - 将 UTF-16 视为固定的 16 位编码会带来什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5066528/

相关文章:

php - 如何使用 PHP 替换阿拉伯字母的初始形式?

java - 在 Java 中将文件保存为双字节 unicode?

java - 一个 21 字节的 UTF-8 序列如何来自 5 个字符?

c++ - HttpSendRequest - 不支持 Unicode 的 POST 数据

Linux 和 Windows 上的 Python sys.maxint、sys.maxunicode

python - 打印 unicode 字符串与打印每个字符,有什么不匹配的地方?

python - 如何在 xpath 字符串中使用 unicode? (统一码编码错误)

c++ - UTF-16BE 到 UTF-8 使用 Boost.Locale 产生垃圾

unicode - 如何在golang中将utf16文本文件读取为字符串?

delphi - 将以null结尾的内存流转换为unicode字符串