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.
unsigned char tmp[] = {0x05, 0x08};
unsigned short polinomial = 0x0589;
unsigned short crc = checksum_calculateCRC16(tmp, 2,polinomial);
memcpy( tmp, &crc, 2 );
return crc;
unsigned char tmp[] = {0x05, 0x08};
unsigned short polinomial = 0x0589;
unsigned short crc = checksum_calculateCRC16(tmp, 2,polinomial);
memcpy( tmp, &crc, 2 );
return crc;
To copy to clipboard, switch view to plain text mode
unsigned short checksum_calculateCRC16(unsigned char data[], unsigned short length, unsigned short polynomial)
{
unsigned char j, xor_flag, bit_mask, byte; // bit counter, XOR flag, bit mask, current byte
unsigned short i; // byte counter
unsigned short total_length = length + 2; // original length + two 0x00 bytes
unsigned short 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;
}
unsigned short checksum_calculateCRC16(unsigned char data[], unsigned short length, unsigned short polynomial)
{
unsigned char j, xor_flag, bit_mask, byte; // bit counter, XOR flag, bit mask, current byte
unsigned short i; // byte counter
unsigned short total_length = length + 2; // original length + two 0x00 bytes
unsigned short 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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks