pythoncrccrc16

What do I wrong with crc16-x25 calculation in python?


I have this code in python 3 to calculate CRC16-X25:

import crcmod


# CRC function using the CRC-16-CCITT standard
crc16 = crcmod.mkCrcFun(0x11021, initCrc=0xFFFF, xorOut=0xFFFF, rev=True)

def calculate_crc(data):
    return crc16(data)

hex_data = '010e00180510100b1b020100000100ff'
    
# Convert the hex string to bytes
binary_data = bytes.fromhex(hex_data)

# Calculate the CRC
crc_value = calculate_crc(binary_data)

# Print the CRC value in hexadecimal format
print(f'CRC value: {crc_value:04X}')

I am not sure if rev should be True or False, so anyway I try both.

But in none of the case I could get the expected answer which 0xAF47.

However when I calculate with this online tool calculate crc16-x25,

in CRC-16/X-25 it does give 0xAF47.

enter image description here

So, what is wrong with my python code?

BTW: crcmod.predefined.Crc('x-25') DO gives the expected result. What is wrong in mkCrcFun(0x11021, initCrc=0xFFFF, xorOut=0xFFFF, rev=True)?


Solution

  • Use crcmod.mkCrcFun(0x11021, initCrc=0, xorOut=0xFFFF, rev=True). The initCrc parameter in mkCrcFun() is actually the exclusive-or of the init and xorout parameters you will see in other definitions, such as the one at https://reveng.sourceforge.io/crc-catalogue/16.htm#crc.cat.crc-16-ibm-sdlc .