根据维基百科UTF-8页面,我从人们那里听到了相互矛盾的观点。
它们是一样的,不是吗?有人能澄清一下吗?
根据维基百科UTF-8页面,我从人们那里听到了相互矛盾的观点。
它们是一样的,不是吗?有人能澄清一下吗?
当前回答
扩展一下其他人给出的答案:
我们有许多语言和许多字符,计算机应该理想地显示这些字符。Unicode为每个字符分配一个唯一的数字或码位。
计算机处理字节之类的数字。这里略过一点历史并忽略内存寻址问题,8位计算机将8位字节视为硬件上容易表示的最大数字单位,16位计算机将其扩展为两个字节,等等。
Old character encodings such as ASCII are from the (pre-) 8-bit era, and try to cram the dominant language in computing at the time, i.e. English, into numbers ranging from 0 to 127 (7 bits). With 26 letters in the alphabet, both in capital and non-capital form, numbers and punctuation signs, that worked pretty well. ASCII got extended by an 8th bit for other, non-English languages, but the additional 128 numbers/code points made available by this expansion would be mapped to different characters depending on the language being displayed. The ISO-8859 standards are the most common forms of this mapping; ISO-8859-1 and ISO-8859-15 (also known as ISO-Latin-1, latin1, and yes there are two different versions of the 8859 ISO standard as well).
但是,当您想要表示来自多种语言的字符时,这是不够的,所以将所有可用字符塞进一个字节是行不通的。
本质上有两种不同类型的编码:一种是通过添加更多位来扩大值范围。这些编码的例子是UCS2(2字节= 16位)和UCS4(4字节= 32位)。它们与ASCII和ISO-8859标准存在本质上相同的问题,因为它们的值范围仍然有限,即使限制要高得多。
The other type of encoding uses a variable number of bytes per character, and the most commonly known encodings for this are the UTF encodings. All UTF encodings work in roughly the same manner: you choose a unit size, which for UTF-8 is 8 bits, for UTF-16 is 16 bits, and for UTF-32 is 32 bits. The standard then defines a few of these bits as flags: if they're set, then the next unit in a sequence of units is to be considered part of the same character. If they're not set, this unit represents one character fully. Thus the most common (English) characters only occupy one byte in UTF-8 (two in UTF-16, 4 in UTF-32), but other language characters can occupy six bytes or more.
多字节编码(在上面的解释之后,我应该说多单元编码)的优点是它们相对节省空间,但缺点是查找子字符串、比较等操作都必须在执行这些操作之前将字符解码为unicode码点(尽管有一些快捷方式)。
UCS标准和UTF标准都对Unicode中定义的代码点进行编码。理论上,这些编码可以用来编码任何数字(在编码支持的范围内)——当然,这些编码是用来编码Unicode码点的。这就是它们之间的关系。
Windows将所谓的“Unicode”字符串处理为UTF-16字符串,而现在大多数unix默认为UTF-8。像HTTP这样的通信协议最适合使用UTF-8,因为UTF-8中的单位大小与ASCII中的单位大小相同,而且大多数此类协议都是在ASCII时代设计的。另一方面,UTF-16在表示所有现存语言时提供了最佳的平均空间/处理性能。
Unicode标准定义的代码点比能用32位表示的要少。因此,出于所有实际目的,UTF-32和UCS4变成了相同的编码,因为您不太可能必须在UTF-32中处理多单元字符。
希望这能补充一些细节。
其他回答
Unicode只是一个标准,它定义了一个字符集(UCS)和编码(UTF)来编码这个字符集。但一般来说,Unicode指的是字符集,而不是标准。
在5分钟内阅读每个软件开发人员绝对必须知道的关于Unicode和字符集(没有借口!)和Unicode的绝对最小值。
UTF-8是一种使用8位序列编码Unicode字符的方法。
Unicode是一种用于表示来自多种语言的各种字符的标准。
不幸的是,“Unicode”根据上下文以各种不同的方式使用。它最正确的用法(IMO)是作为编码字符集——即一组字符以及字符与表示它们的整数码位之间的映射。
UTF-8是一种字符编码——一种将字节序列转换为字符序列的方法,反之亦然。它涵盖了整个Unicode字符集。ASCII编码为每个字符一个字节,其他字符根据其确切的码位占用更多字节(当前定义的所有码位最多4个字节,即最多U-0010FFFF,实际上4个字节可以处理最多U-001FFFFF)。
当“Unicode”被用作字符编码的名称时(例如,作为. net编码。Unicode属性)通常表示UTF-16,它将大多数常见字符编码为两个字节。一些平台(特别是。net和Java)使用UTF-16作为它们的“原生”字符编码。如果您需要担心不能在单个UTF-16值中编码的字符(它们被编码为“代理对”),这将导致一些棘手的问题——但大多数开发人员从不担心这一点,IME。
关于Unicode的一些参考:
Unicode联盟网站,特别是教程部分 乔尔的文章 我自己的文章(面向. net)
它们是一样的,不是吗?
不,他们不是。
我认为你引用的维基百科页面的第一句话给出了一个很好的,简短的总结:
UTF-8是一种可变宽度字符编码,能够使用一到四个8位字节编码Unicode中的所有1,112,064个有效代码点。
阐述:
Unicode is a standard, which defines a map from characters to numbers, the so-called code points, (like in the example below). For the full mapping, you can have a look here. ! -> U+0021 (21), " -> U+0022 (22), \# -> U+0023 (23) UTF-8 is one of the ways to encode these code points in a form a computer can understand, aka bits. In other words, it's a way/algorithm to convert each of those code points to a sequence of bits or convert a sequence of bits to the equivalent code points. Note that there are a lot of alternative encodings for Unicode.
乔尔给出了一个非常好的解释,并概述了这里的历史。
如果我可以总结一下我从这篇文章中收集到的信息:
Unicode将字符分配给序数(十进制形式)。(这些数字被称为码位。)
à -> 224
UTF-8是一种将这些序数(十进制形式)“转换”为二进制表示的编码。
224 -> 11000011 10100000
注意,我们讨论的是224的二进制表示,而不是它的二进制形式,即0b11100000。