编码和解码 Base16

base64 模块还包括 Base16 的编码和解码功能。基数 16 最常被称为十六进制。这些函数与 Base64 和 Base32 函数非常相似:

import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base16 Encode the bytes
e = base64.b16encode(b)
# Decoding the Base16 bytes to string
s1 = e.decode("UTF-8")
# Printing Base16 encoded string
print("Base16 Encoded:", s1)
# Encoding the Base16 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the Base16 bytes
d = base64.b16decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)

这将产生以下输出:

Base16 Encoded: 48656C6C6F20576F726C6421
Hello World!