我正在尝试使用 crcmod 库在 Python 中实现以下 matlab crc 检查算法:
crc8 = comm.CRCGenerator('Polynomial','z^8 + z^2 + z + 1', 'InitialConditions',1,'DirectMethod',true,'FinalXOR',1);
codeword = crc8([0; 0; 1; 0; 0; 1; 1; 1; 1; 0; 1; 0; 0; 1; 1; 1]);
crc= codeword(end-8+1:end)';
预期结果应为:10010001(二进制)
我根据这个comment在python中以以下方式实现了它:
msg = "0010011110100111"
crcx = crcmod.mkCrcFun(0x107, rev=False, initCrc=0xFF, xorOut=0xFF)
#interpret string as binary, convert to bytes, calculate CRC
data = int(msg, 2).to_bytes((len(msg) + 7) // 8, 'big')
crc_result = crcx(data)
print(crc_result)
预期结果应该是:10010001(二进制)或145(十进制)
但是我得到:二进制 1000110,70(十进制)。
我不明白我做错了什么。我将不胜感激任何帮助。
crcmod 定义的初始值与标准值不同。 crcmod 期望通常的初始值与最终的异或进行异或。所以在这种情况下应该是
initCRC=0
。
然后你会得到预期的结果,145,或0x91。