Hello, i m new to c++ and low level programming and i need to check uint16 data with an crc with uint8 check sum i only have 3 free dec places for the crc to transfer - max 999(0x3E7).
So i read now some about crc and i guess what i need is a crc8, does anyone knows an algorithm that can fit my purpose or i am taking it the wrong way?
#define POLYNOMIAL 0xD8 /* 11011 followed by 0's */
uint8_t
crcNaive(uint8_t const message)
{
uint8_t remainder;
/*
* Initially, the dividend is the remainder.
*/
remainder = message;
/*
* For each bit position in the message....
*/
for (uint8_t bit = 8; bit > 0; --bit)
{
/*
* If the uppermost bit is a 1...
*/
if (remainder & 0x80)
{
/*
* XOR the previous remainder with the divisor.
*/
remainder ^= POLYNOMIAL;
}
/*
* Shift the next bit of the message into the remainder.
*/
remainder = (remainder << 1);
}
/*
* Return only the relevant bits of the remainder as CRC.
*/
return (remainder >> 4);
}
That is, just crc *everything* that needs it as if it were a byte stream, and it will work on everything. You *can* write a 16 bit crc but it won't work on generic data.