根据维基百科UTF-8页面,我从人们那里听到了相互矛盾的观点。
它们是一样的,不是吗?有人能澄清一下吗?
根据维基百科UTF-8页面,我从人们那里听到了相互矛盾的观点。
它们是一样的,不是吗?有人能澄清一下吗?
当前回答
UTF-8是Unicode文本的一种可能的编码方案。
Unicode是一个范围广泛的标准,它定义了超过140,000个字符,并为每个字符分配一个数字代码(一个码位)。它还定义了如何对文本进行排序、规范化、更改大小写等规则。Unicode中的字符由一个从0到0x10FFFF(包括0x10FFFF)的码位表示,但有些码位是保留的,不能用于字符。
将一串Unicode码位编码成二进制流的方法不止一种。这些被称为“编码”。最直接的编码是UTF-32,它将每个代码点存储为32位整数,每个整数宽为4字节。因为代码点最多只能到0x10FFFF(需要21位),所以这种编码有点浪费。
UTF-8是另一种编码,由于与UTF-32和其他编码相比有许多优点,它正在成为事实上的标准。UTF-8将每个码位编码为1、2、3或4个字节值的序列。ASCII范围内的码位被编码为一个单字节值,以便与ASCII兼容。超出这个范围的代码点分别使用2、3或4个字节,这取决于它们所在的范围。
UTF-8在设计时考虑了这些属性:
ASCII characters are encoded exactly as they are in ASCII, such that an ASCII string is also a valid UTF-8 string representing the same characters. More efficient: Text strings in UTF-8 almost always occupy less space than the same strings in either UTF-32 or UTF-16, with just a few exceptions. Binary sorting: Sorting UTF-8 strings using a binary sort will still result in all code points being sorted in numerical order. When a code point uses multiple bytes, none of those bytes contain values in the ASCII range, ensuring that no part of them could be mistaken for an ASCII character. This is also a security feature. UTF-8 can be easily validated, and distinguished from other character encodings by a validator. Text in other 8-bit or multi-byte encodings will very rarely also validate as UTF-8 due to the very specific structure of UTF-8. Random access: At any point in a UTF-8 string it is possible to tell if the byte at that position is the first byte of a character or not, and to find the start of the next or current character, without needing to scan forwards or backwards more than 3 bytes or to know how far into the string we started reading from.
其他回答
现有的答案已经解释了很多细节,但这里有一个非常简短的答案,有最直接的解释和例子。
Unicode是将字符映射到码点的标准。 每个字符都有一个唯一的编码点(识别号),它是一个像9731这样的数字。
UTF-8是码点的编码。 为了将所有字符存储在磁盘上(在文件中),UTF-8将字符分成最多4个八位字节(8位序列)-字节。 UTF-8是几种编码(表示数据的方法)之一。例如,在Unicode中,(十进制)码位9731表示一个雪人(☃),它在UTF-8中由3个字节组成:E2 98 83
这是一个排序的列表,其中有一些随机的例子。
Unicode是与ISO/IEC 10646一起定义通用字符集(UCS)的标准,UCS是表示几乎所有已知语言所需的所有现有字符的超集。
Unicode为其存储库中的每个字符分配一个名称和一个数字(字符代码或代码点)。
UTF-8编码,是一种在计算机内存中以数字方式表示这些字符的方法。UTF-8将每个码位映射到一个八字节序列(8位字节)
,例如,
UCS字符= Unicode字符
UCS代码点= U+24B62
UTF-8 encoding = F0 A4 AD A2 (hex) = 11110000 10100100 10101101 10100010 (bin)
我已经检查了Gumbo的答案中的链接,我想在这里粘贴那些东西的一部分,以存在于Stack Overflow上。
"...有些人错误地认为Unicode只是一个16位的代码,每个字符占用16位,因此有65,536个可能的字符。实际上,这是不对的。这是关于Unicode最常见的误解,所以如果你这样想,不要难过。
事实上,Unicode有一种不同的思考字符的方式,你必须理解Unicode思考事物的方式,否则就没有意义了。
到目前为止,我们假设一个字母映射到一些你可以存储在磁盘或内存中的位:
A -> 0100 0001
在Unicode中,字母映射到一个被称为码位的东西,这仍然只是一个理论概念。该代码点如何在内存或磁盘上表示则完全是另一回事……”
"...Unicode联盟给每个字母表中的每个柏拉图式的字母都分配了一个神奇的数字,写起来是这样的:U+0639。这个神奇的数字被称为码位。U+表示“Unicode”,数字是十六进制的。U+0639是阿拉伯字母Ain。英文字母A就是U+0041....”
"...假设我们有一个字符串
你好
在Unicode中,对应以下五个编码点:
U+0048 U+0065 U+ 006c U+ 006c U+ 006f。
只是一堆代码点。数字,真的。我们还没有说过如何将其存储在内存中或在电子邮件中表示它……”
"...这就是编码的作用。
Unicode编码最早的想法,导致了关于两个字节的神话,嘿,让我们把这些数字分别存储在两个字节中。所以Hello变成了
00 48 00 65 00 6c 00 6c 00 6f
对吧?别这么快!难道不可能是:
48 00 65 00 6c 00 6c 00 6f 00 ?……”
扩展一下其他人给出的答案:
我们有许多语言和许多字符,计算机应该理想地显示这些字符。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中处理多单元字符。
希望这能补充一些细节。
UTF-8是Unicode文本的一种可能的编码方案。
Unicode是一个范围广泛的标准,它定义了超过140,000个字符,并为每个字符分配一个数字代码(一个码位)。它还定义了如何对文本进行排序、规范化、更改大小写等规则。Unicode中的字符由一个从0到0x10FFFF(包括0x10FFFF)的码位表示,但有些码位是保留的,不能用于字符。
将一串Unicode码位编码成二进制流的方法不止一种。这些被称为“编码”。最直接的编码是UTF-32,它将每个代码点存储为32位整数,每个整数宽为4字节。因为代码点最多只能到0x10FFFF(需要21位),所以这种编码有点浪费。
UTF-8是另一种编码,由于与UTF-32和其他编码相比有许多优点,它正在成为事实上的标准。UTF-8将每个码位编码为1、2、3或4个字节值的序列。ASCII范围内的码位被编码为一个单字节值,以便与ASCII兼容。超出这个范围的代码点分别使用2、3或4个字节,这取决于它们所在的范围。
UTF-8在设计时考虑了这些属性:
ASCII characters are encoded exactly as they are in ASCII, such that an ASCII string is also a valid UTF-8 string representing the same characters. More efficient: Text strings in UTF-8 almost always occupy less space than the same strings in either UTF-32 or UTF-16, with just a few exceptions. Binary sorting: Sorting UTF-8 strings using a binary sort will still result in all code points being sorted in numerical order. When a code point uses multiple bytes, none of those bytes contain values in the ASCII range, ensuring that no part of them could be mistaken for an ASCII character. This is also a security feature. UTF-8 can be easily validated, and distinguished from other character encodings by a validator. Text in other 8-bit or multi-byte encodings will very rarely also validate as UTF-8 due to the very specific structure of UTF-8. Random access: At any point in a UTF-8 string it is possible to tell if the byte at that position is the first byte of a character or not, and to find the start of the next or current character, without needing to scan forwards or backwards more than 3 bytes or to know how far into the string we started reading from.