Creating xor checksum of all bytes in hex string In Python

Question:

While attempting to communicate with an audio processing device called BSS London Blu-80, I discovered I have to send a checksum created by Xoring the message. An example message being sent would be:

0x8d 0x1e 0x19 0x1b 0x83 0x00 0x01 0x01 0x00 0x00 0x00 0x4b 0x00 0x00 0xc2

With 0xc2 being the correct checksum for that message.

"The checksum is a single-byte exclusive or(xor) of all the bytes in the message body."

The message body is that above minus the checksum.

The code I attempt however:

packet = '0x8d 0x1e 0x19 0x1b 0x83 0x00 0x01 0x01 0x00 0x00 0x00 0x4b 0x00 0x00'
xor = 0
i = 0
while i < len(packet):
    xor = xor ^ ord(packet[i])
    i += 1

>>print xor
46
>>print hex(xor)
'0x2e'

Any help will be appreciated.

Asked By: Nicholas

||

Answers:

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 = 'x8dx1ex19x1bx83x00x01x01x00x00x00x4bx00x00'

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.

Answered By: Stefano Sanfilippo

8 Bit Check sum Solution

r=hex(0x37^0x37^0xA0^0x00^0x07^0x63^0x80^0x8A^0x05^0x0C)[2:].upper()

Result-‘C7’

Answered By: Nandha Kumar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.