Creating xor checksum of all bytes in hex string In Python

You have declared packet as the printable representation of the message:

packet = '0x8d 0x1e 0x19 0x1b 0x83 0x00 0x01 0x01 0x00 0x00 0x00 0x4b 0x00 0x00'

so your current message is not [0x8d, 0x1e, ..., 0x00], but ['0', 'x', '8', 'd', ..., '0'] instead. So, first step is fixing it:

packet = '0x8d 0x1e 0x19 0x1b 0x83 0x00 0x01 0x01 0x00 0x00 0x00 0x4b 0x00 0x00'
packet = [chr(int(x, 16)) for x in packet.split(' ')]

Or, you could consider encoding it "right" from the beginning:

packet = '\x8d\x1e\x19\x1b\x83\x00\x01\x01\x00\x00\x00\x4b\x00\x00'

At this point, we can xor, member by member:

checksum = 0
for el in packet:
    checksum ^= ord(el)

print checksum, hex(checksum), chr(checksum)

the checksum I get is 0x59, not 0xc2, which means that either you have calculated the wrong one or the original message is not the one you supplied.

Tags:

Python

Xor