11 | binary encoding: "handheld two Kun pounds copy, mouth cries hot hot hot"?

Programs = Algorithms + Data Structures. Corresponding to the hardware, the algorithm is computer instructions , data structures on the corresponding binary data .

Computers are binary 1 and 0 with the composition, represents all the information . Instruction used in machine code is the binary representation ; memory character string, integer, floating point also.

A quick look at integer binary representation of the text string. How is this going children experiencing garbled, What is the relationship between Unicode and UTF-8.

First, understand binary " Every a binary "

Decimal integer that can be represented by a binary. Correspond to the binary decimal, such as 0011 3 =

9930763-eb99aa4e27bcfccd.png

To the decimal number 13, is converted into a binary division short, = 1101

9930763-e051e3b7ca3fbacd.png

Negative: leftmost one, as is the corresponding sign, such as 0 is a positive number, a negative

0011 = +3. 1011 = -3 complement . Integer original code notation . Drawback 1000 represents 0, 0000 also represents 0.

Another notation = -5:

9930763-b79c006497928e5e.png

1 must be the highest negative, the highest bit 0 must be positive . 0000 denotes 0, 1000 indicates -8 . 4-bit binary numbers, which may be from 7 -8 to 16 integer, not a waste.

 -5 + 1 = -4,-5 + 6 = 1。

9930763-ca14bfcb20203502.png

字符串的表示,从编码到数字,都可用二进制表示。

二、ASCII 码

8 位二进制,表示需要所有字符(American Standard Code for Information Interchange,美国信息交换标准代码)。

9930763-24487a8b1f9a2113.png

ASCII 码就好比字典, 128 个不同数8 位二进制映射128 不同字符里。a 在 ASCII 里是第 97 个,二进制 0110 0001,十六进制 61。A第 65 个,二进制0100 0001,十六进制41。

ASCII ,9 不是 0000 1001,而是 0011 100115 不是用 0000 1111,两个字符 1 和 5 连续放在一起,就是 0011 0001 和 0011 0101,用两个 8 位表示。

最大的 32 位整数,2147483647。用整数只需 32 位。用字符串(多占空间)有 10 个字符,每个字符用 8 位的话,需要整整 80 位

存储数据用二进制序列化,不是简单地把数据通过 CSV 或者 JSON进行序列化。不管是整数也好,浮点数也好,采用二进制序列化会比存储文本省下不少空间。

ASCII 码只表示了 128 个字符不太够用。于是创建了对应的字符集(Charset)和字符编码(Character Encoding)。

字符集,字符的集合。比如“中文(《新华字典》里面出现的所有汉字)”就是一个字符集,“”, Unicode是字符集,150 种语言14 万个不同的字符。

字符编码:字符(字符集里)用二进制表示的字典。 Unicode可用 UTF-8、UTF-16编码存储成二进制。有了 Unicode可用不止 UTF-8 一种编码形式,知道编码规则,可以正常传输、显示代码。

9930763-85acb8a8897167cf.png

同样文本不同编码存储。另一个程序,不同的编码方式来进行解码和展示,就会乱码。就像密语通信,用错密码本不知所云。

9930763-06e997b8e1998507.png

搜索邮件历史出现了“锟斤拷”

想要 Unicode 编码记录文本,但这些字符在 Unicode 中并不存在。Unicode 统一记录为 U+FFFD 编码。如果用 UTF-8 的格式存储下来,就是\xef\xbf\xbd。如果连续两个这样的字符放在一起,\xef\xbf\xbd\xef\xbf\xbd,这个时候,如果程序把这个字符,用 GB2312 的方式进行 decode,就会变成“锟斤拷”。这就好比我们用 GB2312 这本密码本,去解密别人用 UTF-8 加密的信息,自然没办法读出有用的信息。

而“烫烫烫”,则是因为如果你用了 Visual Studio 的调试器,默认使用 MBCS 字符集。“烫”在里面是由 0xCCCC 来表示的,而 0xCC 又恰好是未初始化的内存的赋值。于是,在读到没有赋值的内存地址或者变量的时候,电脑就开始大叫“烫烫烫”了。

课后思考

负数是原码表示,应该如何处理?如果是补码表示的呢?请你用二进制加法试着算一算,-5+4=-1,通过原码和补码是如何进行的?

[-5 + 4] = complement [--5] Complement + [4] Complement = [1011 + 0100] Complement = [1111] 1001 the complement of the original code

Reproduced in: https: //www.jianshu.com/p/e7d5d8a066ad

Guess you like

Origin blog.csdn.net/weixin_33795833/article/details/91242752