字节数组 ->字符串
bytes.decode()
a = bytes([0x30, 0x31, 0x32, 0x33])
b = a.decode('ascii')
print(b)
# 输出结果
# 0123
字符串 -> 字节数组
str.encode('utf-8')
import binascii
a = '0123'
b = a.encode('ascii')
print(binascii.hexlify(b))
# 输出结果
# b'30313233'
可变字节数组 -> 字符串
bytes和string类型一样为不可变类型,bytes对应的可变类型为bytearray
a = bytearray([0x30, 0x31, 0x32, 0x33])
a.append(0x34)
a.append(0x35)
b = a.decode('ascii')
print(b)
# 输出内容
# 012345
网友评论