根据维基百科UTF-8页面,我从人们那里听到了相互矛盾的观点。
它们是一样的,不是吗?有人能澄清一下吗?
根据维基百科UTF-8页面,我从人们那里听到了相互矛盾的观点。
它们是一样的,不是吗?有人能澄清一下吗?
当前回答
它们是一样的,不是吗?
不,他们不是。
我认为你引用的维基百科页面的第一句话给出了一个很好的,简短的总结:
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.
乔尔给出了一个非常好的解释,并概述了这里的历史。
其他回答
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是一种表示多种人类语言字符的标准。 UTF-8是一种编码Unicode字符的方法。
是的:我故意忽略了UTF-8的内部工作原理。
不幸的是,“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)
Unicode只是一个标准,它定义了一个字符集(UCS)和编码(UTF)来编码这个字符集。但一般来说,Unicode指的是字符集,而不是标准。
在5分钟内阅读每个软件开发人员绝对必须知道的关于Unicode和字符集(没有借口!)和Unicode的绝对最小值。
让我用一个例子来说明这个话题:
A Chinese character: 汉
its Unicode value: U+6C49
convert 6C49 to binary: 01101100 01001001
目前还没有什么神奇的,很简单。现在,假设我们决定将这个字符存储在硬盘驱动器上。为此,我们需要以二进制格式存储字符。我们可以简单地将其存储为'01101100 01001001'。完成了!
但是等一下,'01101100 01001001'是一个字符还是两个字符?你知道这是一个字符,因为我告诉过你,但当计算机读取它时,它不知道。所以我们需要某种编码来告诉计算机把它当做一个。
这就是UTF-8规则的用武之地:https://www.fileformat.info/info/unicode/utf8.htm
Binary format of bytes in sequence
1st Byte 2nd Byte 3rd Byte 4th Byte Number of Free Bits Maximum Expressible Unicode Value
0xxxxxxx 7 007F hex (127)
110xxxxx 10xxxxxx (5+6)=11 07FF hex (2047)
1110xxxx 10xxxxxx 10xxxxxx (4+6+6)=16 FFFF hex (65535)
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (3+6+6+6)=21 10FFFF hex (1,114,111)
根据上面的表格,如果我们想要使用UTF-8格式存储这个字符,我们需要给我们的字符加上一些'headers'前缀。我们的中文字符有16位长(你自己计算二进制值),所以我们将在第三行使用该格式,因为它提供了足够的空间:
Header Place holder Fill in our Binary Result
1110 xxxx 0110 11100110
10 xxxxxx 110001 10110001
10 xxxxxx 001001 10001001
将结果写在一行中:
11100110 10110001 10001001
这是UTF-8二进制值的汉字!你自己看看:https://www.fileformat.info/info/unicode/char/6c49/index.htm
总结
A Chinese character: 汉
its Unicode value: U+6C49
convert 6C49 to binary: 01101100 01001001
encode 6C49 as UTF-8: 11100110 10110001 10001001
附注:如果你想用Python学习本主题,请点击这里。