I can't get what I am doing wrong... The test code is:
#!/usr/bin/python3
data = bytearray(b'\x01\x04\x00\x84\x00\x02')
crc = "0xe231"
data.append(int(crc[4:6], 16))
data.append(int(crc[2:4], 16))
print(data)
Then what I get is:
bytearray(b'\x01\x04\x00\x84\x00\x021\xe2')
Why append is not working? It should be "\x01\x04\x00\x84\x00\x02\x31\xe2"
CodePudding user response:
As @Tomerikoo has pointed out in a comment, the \x31 byte that you think you are missing is actually there. It's just not printed as \x31 but simply as 1 because Pythons prints readable bytes in the readable representation. \x31 is the character 1 in ASCII encoding.
CodePudding user response:
Funny... The print just confused me. I tried:
#!/usr/bin/python3
data = bytearray(b'\x01\x04\x00\x84\x00\x02')
crc = "0xe231"
data.append(int(crc[4:6], 16))
data.append(int(crc[2:4], 16))
print(data)
x = 0
while x < 8:
print(hex(data[x]))
x = 1
Then I could see that the array is correct. Thanks!