2025-09-10:仓库迁移
This commit is contained in:
21
6.数据编码与处理/9.编码和解码十六进制数字.py
Normal file
21
6.数据编码与处理/9.编码和解码十六进制数字.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import base64, binascii
|
||||
|
||||
if __name__ == '__main__':
|
||||
s = b'Hello World!'
|
||||
# 将字节串以十六进制编码
|
||||
h = binascii.b2a_hex(s)
|
||||
print(h)
|
||||
|
||||
# 将数据从十六进制解码成字节串
|
||||
b = binascii.a2b_hex(h)
|
||||
print(b)
|
||||
|
||||
# 同理,在熟悉的base64模块我们也能实现同样的功能
|
||||
h = base64.b16encode(s)
|
||||
print(h)
|
||||
b = base64.b16decode(h)
|
||||
print(b)
|
||||
|
||||
# 个人感觉base64的API更加干净
|
||||
# 对了,如果想要将数据以unicode输出,可以使用一次decode
|
||||
print(h.decode('ascii'))
|
Reference in New Issue
Block a user