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

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"))
posted @ 2019-03-28 17:25  YoungS01  阅读(4286)  评论(0编辑  收藏  举报