I'm trying to XOR all the elements in a bytearray, and generate the resulting checksum value in Hex. Finally, l will append the result to form a new bytearray. Sorry, I'm rusty in python now.
XorMath = bytearray([0x50,0x021,0x45,0x53])
Xor1 = bytearray[0] ^ bytearray[1]
Xor2 = Xor1 ^ bytearray[2]
Xor3 = Xor2 ^ bytearray[3]
checksum = hex(Xor3)
new_array = XorMath.append(checksum)
print(new_array)
Any way to make this more elegant?
You can use
functools.reduceto applyoperator.xorcumulatively to the bytearray. Thehexfunction converts an integer to a hex string, and should be called only when the value is to be displayed rather than stored in a bytearray:This outputs: