Using the function below to compute the crc of unsigned char tmp[] = {0x05, 0x08}, I got "b6 bb". But someone told me the correct result is "9e 17". Who is right? Comments on this issue will be appreciated very much. Thanks in advance.
unsignedshort checksum_calculateCRC16(unsignedchar data[], unsignedshort length, unsignedshort polynomial)
{
unsignedchar j, xor_flag, bit_mask, byte; // bit counter, XOR flag, bit mask, current byte
unsignedshort i; // byte counter
unsignedshort total_length = length + 2; // original length + two 0x00 bytes
unsignedshort remainder = 0xFFFF; // CRC remainder
xor_flag = 0x00;
// Process all bytes
for(i = 0; i < total_length; i++)
{
// Set bit mask for next byte
bit_mask = 0x80;
// Add two bytes with 0x00 after original data
byte = 0x00;
if(i < length)
{
byte = data[i];
}
// Process all bits
for(j = 0; j < 8; j++)
{
// If left-most bit is a 1
if((remainder & 0x8000) == 0x8000)
{
// Set XOR flag
xor_flag = 0x01;
}
// Right-shift remainder
remainder = remainder << 1;
// If current bit is a 1
if((byte & bit_mask) == bit_mask)
{
// Insert a 1 at right-most position of remainder
remainder++;
}
// If XOR flag is set
if(xor_flag == 0x01)
{
// XOR remainder with polynomial
remainder ^= polynomial;
// Clear XOR flag
xor_flag = 0x00;
}
// Shift mask to process next bit
bit_mask = bit_mask >> 1;
}
}
// Return remainder
return remainder;
}