python3中bytes、hex和字符串相互转换

时间:2022-05-08 17:49:55

1、字符串转bytes

a = 'abcd'
a1 = bytes(a,encoding('utf-8'))

2、bytes转字符串

a = b'abcd'
a1 = bytes.decode(a , encoding('utf-8'))

3、16进制字符串转bytes

a='01 02 03 04 05 06'
a1 = a.replace(' ' ,'')
a2 = bytes,fromhex(a1)

 

4、bytes转16进制字符串

"".join(['%02X ' % b for b in bs])