我对编码有点困惑。据我所知,旧的ASCII字符每个字符占用一个字节。一个Unicode字符需要多少字节?
我假设一个Unicode字符可以包含任何语言的所有可能字符——我说的对吗?那么每个字符需要多少字节呢?
UTF-7、UTF-6、UTF-16等是什么意思?它们是Unicode的不同版本吗?
我读了维基百科上关于统一码的文章,但对我来说太难了。我期待看到一个简单的答案。
我对编码有点困惑。据我所知,旧的ASCII字符每个字符占用一个字节。一个Unicode字符需要多少字节?
我假设一个Unicode字符可以包含任何语言的所有可能字符——我说的对吗?那么每个字符需要多少字节呢?
UTF-7、UTF-6、UTF-16等是什么意思?它们是Unicode的不同版本吗?
我读了维基百科上关于统一码的文章,但对我来说太难了。我期待看到一个简单的答案。
当前回答
Unicode是一种为每个字符提供唯一编号的标准。这些唯一的数字被称为代码点(这只是唯一的代码),适用于世界上所有存在的字符(有些字符还有待添加)。
出于不同的目的,您可能需要用字节表示这些代码点(大多数编程语言都是这样做的),这就是字符编码发挥作用的地方。
UTF-8、UTF-16、UTF-32等等都是字符编码,Unicode的码位以不同的方式在这些编码中表示。
UTF-8编码具有可变宽度长度,其中编码的字符可以占用1到4个字节(包括);
UTF-16具有可变长度,其中编码的字符可以占用1或2个字节(即8或16位)。这只代表了称为BMP(基本多语言平面)的所有Unicode字符的一部分,对于几乎所有的情况都足够了。Java对其字符串和字符使用UTF-16编码;
UTF-32有固定的长度,每个字符正好占用4个字节(32位)。
其他回答
简单地说,Unicode是一种为世界上所有字符分配一个数字(称为码位)的标准(它仍在进行中)。
现在你需要用字节表示这些代码点,这叫做字符编码。UTF-8, UTF-16, UTF-6是表示这些字符的方法。
UTF-8是多字节字符编码。字符可以有1到6个字节(其中一些现在可能不需要)。
UTF-32每个字符有4个字节一个字符。
UTF-16为每个字符使用16位,它只表示称为BMP的Unicode字符的一部分(对于所有实际目的来说已经足够了)。Java在其字符串中使用这种编码。
奇怪的是,没有人指出如何计算一个Unicode字符占用多少字节。下面是UTF-8编码字符串的规则:
Binary Hex Comments
0xxxxxxx 0x00..0x7F Only byte of a 1-byte character encoding
10xxxxxx 0x80..0xBF Continuation byte: one of 1-3 bytes following the first
110xxxxx 0xC0..0xDF First byte of a 2-byte character encoding
1110xxxx 0xE0..0xEF First byte of a 3-byte character encoding
11110xxx 0xF0..0xF7 First byte of a 4-byte character encoding
所以简单的答案是:它需要1到4个字节,这取决于第一个将表明它将占用多少字节。
从维基:
UTF-8, 8位可变宽度编码,最大限度地兼容ASCII; UTF-16,一种16位变宽编码; UTF-32, 32位,固定宽度编码。
这是三种最流行的不同编码。
在UTF-8中,每个字符被编码成1到4个字节(主要编码) 在UTF16中,每个字符被编码成1到2个16位的单词和 在UTF-32中,每个字符都被编码为一个32位的单词。
在utf - 8:
1 byte: 0 - 7F (ASCII)
2 bytes: 80 - 7FF (all European plus some Middle Eastern)
3 bytes: 800 - FFFF (multilingual plane incl. the top 1792 and private-use)
4 bytes: 10000 - 10FFFF
在utf - 16:
2 bytes: 0 - D7FF (multilingual plane except the top 1792 and private-use )
4 bytes: D800 - 10FFFF
在utf - 32:
4 bytes: 0 - 10FFFF
根据定义,10FFFF是最后一个unicode码位,这样定义是因为它是UTF-16的技术限制。
它也是UTF-8可以在4字节内编码的最大码点,但UTF-8编码背后的思想也适用于5字节和6字节编码,以覆盖码点,直到7FFFFFFF。只有UTF-32的一半。
在Unicode中,答案是不容易给出的。正如您已经指出的,问题在于编码。
对于任何没有变音符字符的英语句子,UTF-8的答案将是字符的字节数,而UTF-16的答案将是字符数乘以2。
(到目前为止)我们可以声明大小的唯一编码是UTF-32。每个字符总是32位,即使我想象代码点是为未来的UTF-64准备的:)
至少有两件事让它如此困难:
composed characters, where instead of using the character entity that is already accented/diacritic (À), a user decided to combine the accent and the base character (`A). code points. Code points are the method by which the UTF-encodings allow to encode more than the number of bits that gives them their name would usually allow. E.g. UTF-8 designates certain bytes which on their own are invalid, but when followed by a valid continuation byte will allow to describe a character beyond the 8-bit range of 0..255. See the Examples and Overlong Encodings below in the Wikipedia article on UTF-8. The excellent example given there is that the € character (code point U+20AC can be represented either as three-byte sequence E2 82 AC or four-byte sequence F0 82 82 AC. Both are valid, and this shows how complicated the answer is when talking about "Unicode" and not about a specific encoding of Unicode, such as UTF-8 or UTF-16. Strictly speaking, as pointed out in a comment, this doesn't seem to be the case any longer or was even based on a misunderstanding on my part. The quote from the updated Wikipedia article reads: Longer encodings are called overlong and are not valid UTF-8 representations of the code point.