1、字符串转bytes
-
-
-
-
'0123456789ABCDEF0123456789ABCDEF'
-
b'0123456789ABCDEF0123456789ABCDEF'
-
-
-
return bytes(str,encoding='utf8')
2、bytes转字符串
-
-
-
-
b'0123456789ABCDEF0123456789ABCDEF'
-
'0123456789ABCDEF0123456789ABCDEF'
-
-
-
return bytes.decode(bs,encoding='utf8')
3、十六进制字符串转bytes
-
-
-
-
'01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF'
-
b'\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef'
-
-
def hexStringTobytes(str):
-
str = str.replace(" ", "")
-
return bytes.fromhex(str)
-
4、bytes转十六进制字符串
-
-
-
-
b'\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef'
-
'01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF'
-
-
def bytesToHexString(bs):
-
-
-
# hex_str += str(hex(item))[2:].zfill(2).upper() + " "
-
-
return ''.join(['%02X ' % b for b in bs])