python字符串/Bytes/16进制/x01等之间的转换

总结
python字符串/Bytes/16进制/x01等之间的转换_第1张图片

示例代码:

text = "0123456789abcdef"
print(text)
b_text = bytes(text, encoding = "utf8")
print(b_text)
hex_text = binascii.unhexlify(b_text) #a2b_hex
print(hex_text)
b_ctext = binascii.hexlify(hex_text) #b2a_hex
print(b_ctext)
ctext = str(b_ctext, encoding = "utf-8")
print(ctext)

输出

0123456789abcdef
b'0123456789abcdef'
b'\x01#Eg\x89\xab\xcd\xef'
b'0123456789abcdef'
0123456789abcdef

示例代码:

text = "0123456789abcdef"
print(text)
b_text = bytes(text, encoding = "utf8")
print(b_text)
b_ctext = binascii.hexlify(b_text) #b2a_hex
print(b_ctext)
ctext = str(b_ctext, encoding = "utf-8")
print(ctext)
print(ctext.encode())
hex_text = binascii.unhexlify(ctext.encode()) #a2b_hex
print(hex_text)

输出

0123456789abcdef
b'0123456789abcdef'
b'30313233343536373839616263646566'
30313233343536373839616263646566
b'30313233343536373839616263646566'
b'0123456789abcdef'

你可能感兴趣的:(python字符串/Bytes/16进制/x01等之间的转换)