import binascii
# 一个字符串
s = "String"
# 字符串变字符型字节流
s_byte = s.encode()
# 字符型字节流转十六进制字节流, 与b2a_hex(),bytes.hex()方法功能一样
s_hex = binascii.hexlify(s_byte)
# 十六进制字节流转字符型字节流, 与bytes.fromhex()功能一样
s_byte = binary.a2b_hex(s_hex)
另外,binascii 库还提供了编码、解码数据的功能
#0x5c是字符’\’, 这句话就是把字符串数组如[“a”, “b”, “c”]转换为[‘\x61’, ‘\x62’, ‘\x63’]
newli = [hex(ord(ch)).replace('0x', chr(0x5c)+'x') for ch in li]