一、 Python中的编、解码问题
1. byte数组(16进制) 转 字符串
input_byte = [0xFF,0x01,0x02,0x01,0x03,0x05]
print(input_byte)
print(bytes(input_byte))
输出:
[255, 1, 2, 1, 3, 5]
b’\xff\x01\x02\x01\x03\x05’
2. 16进制数组字符串 转 int
在工作中时常需要对 Udp 发来的通信数据进行解码
input_byte = b'\x55\x01\x02\x01\x02\x03\x04'
def Decode(input_byte):
i = int.from_bytes(input_byte[1:3],'big')
print(i,hex(i))
return(i)
Decode(input_byte)
结果输出:
258 0x102