美文网首页
python 字符串,bytes和hex字符串之间的相互转换

python 字符串,bytes和hex字符串之间的相互转换

作者: 忘了呼吸的那只猫 | 来源:发表于2020-07-08 21:31 被阅读0次
import binascii

datastr='13'
#string 类型转换为byte
dataByte=str.encode(datastr)
#byte串 转换为16进制 byte串 ,比如 b'12' 转换为b'3132'
a=binascii.b2a_hex(dataByte)
#16 进制byte串  转换为string串,比如b'3132' 转换为"3132",用来显示
print(a.decode())
#16 进制string  转换为byte串,比如'1112' 转换为b"\x11\x12",用来传输
print(bytes.fromhex("1112"))

相关文章

网友评论

      本文标题:python 字符串,bytes和hex字符串之间的相互转换

      本文链接:https://www.haomeiwen.com/subject/uvihcktx.html