ASCII 码表
回忆上次内容
-
ord
通过 字符
找到对应的 数字
-
chr
通过 数字
找到对应的 字符
-
a
对应 97 -
b
对应 98 -
c
对应 99
- 为什么小写a从97开始?
- 但除了小写字母之外还有很多字符
- 他们都是如何分布的呢?🤔
小写字母
#输出a,b,c
ord("a")
ord("b")
ord("c")
#输出a的相对序号
ord("a")-ord("a")
#输出z-a的数字差距,相对序号
ord("z")-ord("a")
- a、b、c 这些字符是挨着的
- 正好从0到25,总共26个🔢
- 对应数字也是挨着的
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_进制](https://file.cfanz.cn/uploads/png/2022/02/12/2/3N7767H0e1.png)
编码规律
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_git_02](https://file.cfanz.cn/uploads/png/2022/11/11/7/42SP6S696f.png)
字符全排列
- 序号用二进制的方式存在字节(byte)中
- 一个字节(byte)总有8位(bit)
- 每一位(bit)是一个二进制(binary)数字(digit)
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_03](https://file.cfanz.cn/uploads/png/2022/11/11/7/739880K2U9.png)
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_进制_04](https://file.cfanz.cn/uploads/png/2022/11/11/7/fF7d2B2PIK.png)
遍历范围
for i in range(2 ** 8):
print(i,end=",")
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_进制_05](https://file.cfanz.cn/uploads/png/2022/11/11/7/89bc4a4S7G.png)
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_06](https://file.cfanz.cn/uploads/png/2022/11/11/7/3J604IS0TB.png)
对应的字符
for n in range(255):
print(chr(n),end="")
if n % 16 == 0:
print()
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_07](https://file.cfanz.cn/uploads/png/2022/11/11/7/e8f3P8Ocea.png)
结果
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_08](https://file.cfanz.cn/uploads/png/2022/11/11/7/0b68VCJ669.png)
- 字母和数字还是挺完整的
- 这套对应关系有没有个名字呢?
ASCII 码表
-
A
merican S
tandard C
ode for I
nformation I
nterchange
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_09](https://file.cfanz.cn/uploads/png/2022/02/12/2/3EF021eC4W.png)
- 这建立起了
字符
和 二进制01
的 映射关系
字符
和 二进制数
的 映射关系
如果不一致
- 面对同一个二进制数 01010101
- 就会映射到不同的字符
- 人们看到不同的字符就认为是乱码
- 这套ascii标准在各种计算机系统中需要统一
- 否则无法通信
- 这个 ASCII 什么时候开始有的呢?
初现
- 1967 年的时候就有了最初这个 ASCII 码表🔡
- 低电平表示 0
- 高电平表示 1
- 电子计算机中所有的数据都是 0 和 1
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_10](https://file.cfanz.cn/uploads/png/2022/11/11/7/8LW191b3EW.png)
- (American National Standard Institute , ANSI )
- 最初是美国的国家标准
- 被称作美国信息交换标准代码
国际化
- International Organization for Standardization a.k.a. ISO
- 称为 ISO 646 标准
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_11](https://file.cfanz.cn/uploads/png/2022/11/11/7/9e7AJWB1L7.png)
- 能否完整系统地看到整个ascii码表的对应关系呢?
ascii
sudo apt install ascii
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_12](https://file.cfanz.cn/uploads/png/2022/11/11/7/e98Be0e4E2.png)
使用ascii
- Dec 对应的是 10 进制数
- Hex 对应的是 16 进制数
- 最后一列 对应的是 具体字符或功能
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_进制_13](https://file.cfanz.cn/uploads/jpeg/2022/11/11/7/eF3e61cK09.jpeg)
解码 ASCII
- 这个小写的
a
在电脑中存储的时候 - 对应着一个字节
- 向上找到
110
- 这是他的高三位
-
765
位
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_14](https://file.cfanz.cn/uploads/png/2022/02/12/2/NW44H4O1T2.png)
- 向左找到
0001
- 这是他的低四位
-
4321
位 - 在前面加一个
0
- 得到(
01100001
)2进制
总结
-
A
merican S
tandard C
ode for I
nformation I
nterchange
- 数字的编码减去
0x30
正好得到数字本身
-
0x41-0x5A
这个范围是 大
写字母 -
0x61-0x7A
这个范围是 小
写字母 -
0x20-0x7F
之间各处零散排布着各种符号
![图片描述 [oeasy]python0013_ASCII码表_英文字符编码_键盘字符_码表_15](https://file.cfanz.cn/uploads/png/2022/02/12/2/3EF021eC4W.png)
- 字符在计算机当中是用二进制方式存储的
- 我们现在可以得到字符的序号
- 但是能通过字符序号
- 得到字符的二进制形态么?🤔
- 我们下次再说👋
- 蓝桥->https://www.lanqiao.cn/teacher/3584
- github->https://github.com/overmind1980/oeasy-python-tutorial
- gitee->https://gitee.com/overmind1980/oeasypython
- 视频->https://www.bilibili.com/video/BV1CU4y1Z7gQ 作者:oeasy