22 lines
557 B
Python
22 lines
557 B
Python
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'))
|