I am trying to implement in Python, using the crcmod library, the following matlab crc check algorithm:
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)';
The expected result should be: 10010001 (binary)
I have implemented it in the following way in python based on this comment:
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)
The expected result should be: 10010001 (binary) or 145 (decimal)
But instead I get: 1000110 in binary, 70 (decimal).
I don't understand what I am doing wrong. I will appreciate any help with this.
crcmod defines the initial value differently than what is standard. crcmod is expecting the usual initial value exclusive-or'ed with the final xor. So in this case it should be
initCRC=0.Then you get the expected result, 145, or 0x91.