根据维基百科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中处理多单元字符。

希望这能补充一些细节。

其他回答

你通常从谷歌开始,然后想尝试不同的东西。 但是如何打印和转换所有这些字符集呢?

这里我列出了一些有用的一行程序。

Powershell:

# Print character with the Unicode point (U+<hexcode>) using this: 
[char]0x2550

# With Python installed, you can print the unicode character from U+xxxx with:
python -c 'print(u"\u2585")'

如果你有更多的Powershell trix或快捷方式,请评论。

在Bash中,你会喜欢libiconv和util-linux包中的iconv、hexdump和xxd(可能在其他*nix发行版中命名不同)。

# To print the 3-byte hex code for a Unicode character:
printf "\\\x%s" $(printf '═'|xxd -p -c1 -u)
#\xE2\x95\x90

# To print the Unicode character represented by hex string:
printf '\xE2\x96\x85'
#▅

# To convert from UTF-16LE to Unicode
echo -en "════"| iconv -f UTF-16LE -t UNICODEFFFE

# To convert a string into hex: 
echo -en '═�'| xxd -g 1
#00000000: e2 95 90 ef bf bd

# To convert a string into binary:
echo -en '═�\n'| xxd -b
#00000000: 11100010 10010101 10010000 11101111 10111111 10111101  ......
#00000006: 00001010

# To convert a binary string into hex:
printf  '%x\n' "$((2#111000111000000110000010))"
#e38182

这篇文章解释了所有细节 http://kunststube.net/encoding/

写入缓冲区

如果你写入一个4字节的缓冲区,符号あUTF8编码,你的二进制将看起来像这样:

00000000 11100011 10000001 10000010

如果你写入一个4字节的缓冲区,使用UTF16编码的符号あ,你的二进制将看起来像这样:

00000000 00000000 00110000 01000010

正如你所看到的,根据你在内容中使用的语言,这将相应地影响你的记忆。

例如,对于这个特定的符号:あUTF16编码更有效,因为我们有2个空闲字节用于下一个符号。但这并不意味着你必须使用UTF16来表示日本字母。

从缓冲区读取

现在,如果你想读取上面的字节,你必须知道它是用什么编码写的,并正确解码回来。

例:如果你解码这个: 00000000 11100011 10000001 10000010 转换为UTF16编码,你将得到臣而不是あ

注意:Encoding和Unicode是两个不同的东西。Unicode是一个大(表),每个符号都映射到一个唯一的码点。例如,あ符号(字母)有一个(码位):30 42(十六进制)。另一方面,编码是一种将符号转换为更合适的方式的算法,当存储到硬件时。

30 42 (hex) - > UTF8 encoding - > E3 81 82 (hex), which is above result in binary.

30 42 (hex) - > UTF16 encoding - > 30 42 (hex), which is above result in binary.

我已经检查了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 ?……”

它们不是一回事——UTF-8是编码Unicode的一种特殊方式。

根据您的应用程序和您打算使用的数据,有许多不同的编码可供选择。据我所知,最常见的是UTF-8、UTF-16和UTF-32。

如果我可以总结一下我从这篇文章中收集到的信息:

Unicode将字符分配给序数(十进制形式)。(这些数字被称为码位。)

à -> 224

UTF-8是一种将这些序数(十进制形式)“转换”为二进制表示的编码。

224 -> 11000011 10100000

注意,我们讨论的是224的二进制表示,而不是它的二进制形式,即0b11100000。