It's the first time I use bitewise operators and raw binary data in Python and I'm struggling to code a CRC check as defined in a documentation.
The documentation define the CRC check in pseudo-code like this:
GENERATOR = 1111111111111010000001001
MSG = binary("8D4840D6202CC371C32CE0576098") # total 112 bits
FOR i FROM 0 TO 88: # 112 - 24 parity bits
if MSG[i] is 1:
MSG[i:i+24] = MSG[i:i+24] ^ GENERATOR
CRC = MSG[-24:] # last 24 bits
IF CRC not 0:
MSG is corrupted
So far I've coded this:
adsb_hex = "8D4840D6202CC371C32CE0576098"
adsb_dec = int(adsb_hex, 16)
adsb_bin = bin(adsb_dec)[2:].zfill(8)
generator = 0b1111111111111010000001001
adsb_bin_list = [int((adsb_dec >> bit) & 1) for bit in range(112 - 1, -1, -1)]
check = []
for i in range(88):
curr_bit = adsb_bin_list[i]
if curr_bit is 1:
check[i:i+24] = int(adsb_bin_list[i:i+24]) ^ generator
crc = check[-24:]
I don't know the Pythonic way to correctly do:
MSG[i:i+24] = MSG[i:i+24] ^ GENERATOR
CRC = MSG[-24:]
How can I do it correctly, the Pythonic way?
inttostrin statementcheck[i:i+24] = int(adsb_bin_list[i:i+24]) ^ generator