So I'm trying to implement a serial line data protocol. One of the commands has hex value 0xf031 (61489 in integer) .
When I try to enter it in as a unsigned short value in a struct I get this as an output:
d1= 61489
d2 = 24
pack('<HH', d1, d2)
>> b'1\xf0\x18\x00'
I expect
b'\x31\xf0\x18\x00'
With some trail and error I found out that the highest value it accepts is 61448
d1= 61448
d2 = 24
print(pack('<HH', d1, d2))
>> b'\x08\xf0\x18\x00'
Have anyone any tips or tricks. Even If i split the two bytes and enter them as single bytes using B (unsigned char) i get the same result.
d1= hex(61489)
d2 = 24
#print(d1)
d1_1 = d1[2:4]
d1_2 = d1[4:]
print(pack('<BBH', int(d1_1, base=16), int(d1_2,base = 16), d2))
>> b'\xf01\x18\x00'
And again the value 61448 gives correct bytes.
Just to show what 61449 gives me
d1= hex(61449)
d2 = 24
#print(d1)
d1_1 = d1[2:4]
d1_2 = d1[4:]
print(pack('<BBH', int(d1_1, base=16), int(d1_2,base = 16), d2))
>> b'\xf0\t\x18\x00'
Im using Jupyter-Lab in pipenv. python 3.8.7
CodePudding user response:
You're seeing what you should be seeing. The 1
in the b-string isn't the value 1, it's the ASCII character "1" (0x31). b-strings will represent as printable characters when possible.
You can see if you convert it back that the original value is still there (in decimal):
>>> unpack('<H', pack('<H', 0xf031))
(61489,)