我在 python 3 中有这段代码来计算 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}')
我不确定 rev 应该是 True 还是 False,所以无论如何我都尝试了。
但无论如何我都无法得到预期的答案
0xAF47
。
但是当我用这个在线工具计算计算crc16-x25,
在
CRC-16/X-25
中它确实给出了0xAF47
。
那么,我的Python代码有什么问题吗?
顺便说一句:
crcmod.predefined.Crc('x-25')
DO 给出了预期的结果。 mkCrcFun(0x11021, initCrc=0xFFFF, xorOut=0xFFFF, rev=True)
有什么问题吗?
使用
crcmod.mkCrcFun(0x11021, initCrc=0, xorOut=0xFFFF, rev=True)
。