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

0 评论
/ /
603 阅读
/
1172 字
27 2020-12

总结

1111111.png

示例代码

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'