Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created March 14, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigderington/f820d12e0b33c4002a4c36f67ed82436 to your computer and use it in GitHub Desktop.
Save craigderington/f820d12e0b33c4002a4c36f67ed82436 to your computer and use it in GitHub Desktop.
Checksum XOR
# -*- coding: utf-8 -*-byte1 ^ byte2 ^
# xor_final.py
# vars
checksum = 0
checksum_xor = 0
n = 2
# raw data
radio_09010 = '02040d3031000010723320000a100000363303'
radio_10232 = '02040d3031000010027f80000fc00000324103'
radio_10253 = '02040d30310000100280d01869f00000434203'
# decoded
radio_data_9010 = radio_09010[2:32] #'040d3031000010027f80000fc00000'
radio_data_10232 = radio_10232[2:32] #'040d3031000010027f80000fc00000'
radio_data_10253 = radio_10253[2:32] #'040d30310000100280d01869f00000'
print('Data (in hex): {}'.format(radio_data_9010))
print('Data (in hex): {}'.format(radio_data_10232))
print('Data (in hex): {}'.format(radio_data_10253))
# checksum
data_checksum_1 = '3633'.decode('hex')
data_checksum_2 = '3241'.decode('hex')
data_checksum_3 = '4342'.decode('hex')
print('Data Packet Checksum 1: {}'.format(data_checksum_1))
print('Data Packet Checksum 2: {}'.format(data_checksum_2))
print('Data Packet Checksum 3: {}'.format(data_checksum_3))
# break data into bytes
bytes1 = [radio_data_9010[i:i+n] for i in range(0, len(radio_data_9010), n)]
bytes2 = [radio_data_10232[i:i+n] for i in range(0, len(radio_data_10232), n)]
bytes3 = [radio_data_10253[i:i+n] for i in range(0, len(radio_data_10253), n)]
for byte in bytes3:
checksum_xor ^= int(byte, 16)
print(checksum_xor)
checksum_xor = '{0:02x}'.format(checksum_xor)
checksum_case = checksum_xor.upper()
print('Checksum XOR: {}'.format(checksum_case))
print('Valid Checksum: {}'.format(data_checksum_1 == checksum_case))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment