zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Python转换 字符串 之间 16 进制 bytes
2023-09-27 14:26:32 时间

总结
在这里插入图片描述

示例代码:

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'