The evolution history of ANSI, UNICODE, UTF-8

byte:

A long time ago, there was a group of people who decided to use 8 transistors that could be switched on and off to combine into different states to represent everything in the world. They see that 8 switch states are good, so they call this a "byte".

computer:

Later, they made some machines that could process these bytes. The machine started, and the bytes could be used to combine many states, and the states began to change. They saw that this was good, so they called the machine a "computer".

Control code:

Initially the computer will only be used in the United States. Eight-bit bytes can be combined into a total of 256 (2 to the 8th power) different states.

They defined the 32 states whose numbers start from 0 for special purposes. Once the bytes agreed on by the terminal and the printer are passed, some agreed actions must be performed. When it encounters 00x10, the terminal will wrap the line. When it encounters 0x07, the terminal will beep to people. For example, if it encounters 0x1b, the printer will print the words in reverse, or the terminal will display letters in color. They see that this is good, so they call these byte states below 0x20 "control codes".

ASCII:

They also represented all spaces, punctuation marks, numbers, and uppercase and lowercase letters with consecutive byte states, until the number 127, so that the computer could use different bytes to store English text. Everyone feels good when they see this, so everyone calls this scheme the ANSI "Ascii" encoding (American Standard Code for Information Interchange). All the computers in the world at the time used the same ASCII scheme to store English text.

Please refer to the ASCII code table for a detailed explanation

Extended character set:

Later, just like the building of the Tower of Babylon, computers all over the world began to be used, but many countries did not use English, and many of their letters were not in ASCII. In order to save their words in the computer, they decided to use 127 The space after the number represents these new letters and symbols, and many shapes such as horizontal lines, vertical lines, and crosses that need to be used when drawing tables are added, and the serial number has been numbered to the last state 255. The character set on pages 128 to 255 is called the "extended character set". Since then, there will be no new state for greedy human beings to use. The US imperialists may not have thought that people in third world countries also hope to use computers!

GB2312:

When Chinese people got computers, there was no byte state that could be used to represent Chinese characters, and there were more than 6,000 commonly used Chinese characters that needed to be saved. However, this cannot be difficult for the wise Chinese people. We rudely cancel the strange symbols after the number 127, and stipulate that the meaning of a character smaller than 127 is the same as the original one, but two characters larger than 127 are connected together. When , it means a Chinese character, the first byte (he calls it high byte) goes from 0xA1 to 0xF7, and the next byte (low byte) goes from 0xA1 to 0xFE, so we can combine more than 7000 Simplified Chinese characters. In these codes, we also compiled mathematical symbols, Roman and Greek letters, and Japanese pseudonyms. Even the numbers, punctuation, and letters that were originally in ASCII were all re-coded into two-byte long codes. , which is often referred to as "full-width" characters, and those below 127 are called "half-width" characters.

The Chinese people see that this is very good, so they call this Chinese character scheme "GB2312". GB2312 is a Chinese extension to ASCII.

GBK standard:

But there are too many Chinese characters in China, and we soon found that there are many people whose names cannot be typed here, especially some national leaders who are very troublesome to others. So we have to continue to find out the unused code points of GB2312 and use them honestly.

后来还是不够用,于是干脆不再要求低字节一定是127号之后的内码,只要第一个字节是大于127就固定表示这是一个汉字的开始,不管后面跟的是不是扩展字符集里的内容。结果扩展之后的编码方案被称为 GBK 标准,GBK 包括了 GB2312 的所有内容,同时又增加了近20000个新的汉字(包括繁体字)和符号。

UNICODE:

后来少数民族也要用电脑了,于是我们再扩展,又加了几千个新的少数民族的字,GBK 扩成了 GB18030。从此之后,中华民族的文化就可以在计算机时代中传承了。
中国的程序员们看到这一系列汉字编码的标准是好的,于是通称他们叫做 "DBCS"(Double Byte Charecter Set 双字节字符集)。在DBCS系列标准里,最大的特点是两字节长的汉字字符和一字节长的英文字符并存于同一套编码方案里,因此他们写的程序为了支持中文处理,必须要注意字串里的每一个字节的值,如果这个值是大于127的,那么就认为一个双字节字符集里的字符出现了。那时候凡是受过加持,会编程的计算机僧侣们都要每天念下面这个咒语数百遍:
"一个汉字算两个英文字符!一个汉字算两个英文字符……"
因为当时各个国家都像中国这样搞出一套自己的编码标准,结果互相之间谁也不懂谁的编码,谁也不支持别人的编码,连大陆和台湾这样只相隔了150海里,使用着同一种语言的兄弟地区,也分别采用了不同的 DBCS 编码方案——当时的中国人想让电脑显示汉字,就必须装上一个"汉字系统",专门用来处理汉字的显示、输入的问题,但是那个台湾的愚昧封建人士写的算命程序就必须加装另一套支持 BIG5 编码的什么"倚天汉字系统"才可以用,装错了字符系统,显示就会乱了套!这怎么办?而且世界民族之林中还有那些一时用不上电脑的穷苦人民,他们的文字又怎么办?
真是计算机的巴比伦塔命题啊!
正在这时,大天使加百列及时出现了——一个叫 ISO (国际标谁化组织)的国际组织决定着手解决这个问题。他们采用的方法很简单:废了所有的地区性编码方案,重新搞一个包括了地球上所有文化、所有字母和符号的编码!他们打算叫它"Universal Multiple-Octet Coded Character Set",简称 UCS, 俗称 "UNICODE"。
UNICODE 开始制订时,计算机的存储器容量极大地发展了,空间再也不成为问题了。于是 ISO 就直接规定必须用两个字节,也就是16位来统一表示所有的字符,对于ascii里的那些“半角”字符,UNICODE 包持其原编码不变,只是将其长度由原来的8位扩展为16位,而其他文化和语言的字符则全部重新统一编码。由于"半角"英文符号只需要用到低8位,所以其高 8位永远是0, 因此这种大气的方案在保存英文文本时会多浪费一倍的空间。
这时候,从旧社会里走过来的程序员开始发现一个奇怪的现象:他们的 strlen函数靠不住了,一个汉字不再是相当于两个字符了,而是一个!是的,从 UNICODE 开始,无论是半角的英文字母,还是全角的汉字,它们都是统一的"一个字符"!同时,也都是统一的"两个字节",请注意"字符"和"字节"两个术语的不同, “字节”是一个8位的物理存贮单元,而“字符”则是一个文化相关的符号。在UNICODE 中,一个字符就是两个字节。一个汉字算两个英文字符的时代已经快过去了。

从前多种字符集存在时,那些做多语言软件的公司遇上过很大麻烦,他们为了在不同的国家销售同一套软件,就不得不在区域化软件时也加持那个双字节字符集咒语,不仅要处处小心不要搞错,还要把软件中的文字在不同的字符集中转来转去。UNICODE 对于他们来说是一个很好的一揽子解决方案,于是从 Windows NT 开始,MS 趁机把它们的操作系统改了一遍,把所有的核心代码都改成了用 UNICODE 方式工作的版本,从这时开始,WINDOWS 系统终于无需要加装各种本土语言系统,就可以显示全世界上所有文化的字符了。

UTF8:

但是,UNICODE 在制订时没有考虑与任何一种现有的编码方案保持兼容,这使得 GBK 与UNICODE 在汉字的内码编排上完全是不一样的,没有一种简单的算术方法可以把文本内容从UNICODE编码和另一种编码进行转换,这种转换必须通过查表来进行。
如前所述,UNICODE 是用两个字节来表示为一个字符,他总共可以组合出65535不同的字符,这大概已经可以覆盖世界上所有文化的符号。如果还不够也没有关系,ISO已经准备了UCS-4方案,说简单了就是四个字节来表示一个字符,这样我们就可以组合出21亿个不同的字符出来(最高位有其他用途),这大概可以用到银河联邦成立那一天吧!
UNICODE 来到时,一起到来的还有计算机网络的兴起,UNICODE 如何在网络上传输也是一个必须考虑的问题,于是面向传输的众多 UTF(UCS Transfer format)标准出现了,顾名思义,UTF8就是每次8个位传输数据,而UTF16就是每次16个位,只不过为了传输时的可靠性,从UNICODE到 UTF时并不是直接的对应,而是要过一些算法和规则来转换。
受到过网络编程加持的计算机僧侣们都知道,在网络里传递信息时有一个很重要的问题,就是对于数据高低位的解读方式,一些计算机是采用低位先发送的方法,例如我们PC机采用的 INTEL 架构,而另一些是采用高位先发送的方式,在网络中交换数据时,为了核对双方对于高低位的认识是否是一致的,采用了一种很简便的方法,就是在文本流的开始时向对方发送一个标志符——如果之后的文本是高位在位,那就发送"FEFF",反之,则发送"FFFE"。不信你可以用二进制方式打开一个UTF-X格式的文件,看看开头两个字节是不是这两个字节?
讲到这里,我们再顺便说说一个很著名的奇怪现象:当你在 windows 的记事本里新建一个文件,输入"联通"两个字之后,保存,关闭,然后再次打开,你会发现这两个字已经消失了,代之的是几个乱码!呵呵, 有人说这就是联通之所以拼不过移动的原因
其实这是因为GB2312编码与UTF8编码产生了编码冲撞的原因。
从网上引来一段从 UNICODE到UTF8的转换规则
[cpp]  view plain  copy
  1. Unicode  
  2. UTF-8  
  3. 0000 - 007F  
  4. 0xxxxxxx  
  5. 0080 - 07FF  
  6. 110xxxxx 10xxxxxx  
  7. 0800 - FFFF  
  8. 1110xxxx 10xxxxxx 10xxxxxx  

例如"汉"字的Unicode编码是6C49。6C49在0800-FFFF之间,所以要用3字节模板:1110xxxx 10xxxxxx 10xxxxxx。将6C49写成二进制是:0110 1100 0100 1001,将这个比特流按三字节模板的分段方法分为0110 110001 001001,依次代替模板中的x,得到:1110-0110 10-110001 10-001001,即E6 B1 89,这就是其UTF8的编码。
而当你新建一个文本文件时,记事本的编码默认是ANSI, 如果你在ANSI的编码输入汉字,那么他实际就是GB系列的编码方式,在这种编码下,"联通"的内码是:
c1 1100 0001
aa 1010 1010
cd 1100 1101
a8 1010 1000
注意到了吗?第一二个字节、第三四个字节的起始部分的都是"110"和"10",正好与UTF8规则里的两字节模板是一致的,于是再次打开记事本时,记事本就误认为这是一个UTF8编码的文件,让我们把第一个字节的110和第二个字节的10去掉,我们就得到了"00001 101010",再把各位对齐,补上前导的0,就得到了"0000 0000 0110 1010",不好意思,这是UNICODE的006A,也就是小写的字母"j",而之后的两字节用UTF8解码之后是0368,这个字符什么也不是。这就是只有"联通"两个字的文件没有办法在记事本里正常显示的原因。
而如果你在"联通"之后多输入几个字,其他的字的编码不见得又恰好是110和10开始的字节,这样再次打开时,记事本就不会坚持这是一个utf8编码的文件,而会用ANSI的方式解读之,这时乱码又不出现了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525520&siteId=291194637