I understand how CRC for a byte can be calculated by modulo 2 division with a polynomial but I don't understand how do you calculate CRC for data consisting of byte arrays. CRC for a single byte can be calculated by this following code
#define WIDTH 8
#define TOPBIT 1 << 7
#define POLYNOMIAL 0xD8
uint8_t(uint8_t const message)
{
uint8_t remainder = 0;
remainder ^= message;
for (uint8_t bit = 8; bit > 0; --bit)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
return (remainder);
}
but what about byte array ? I found above code on this site, Author also gave the code for byte array where he just XOR'ed current remainder with next byte
remainder ^= (message[byte] << (WIDTH - 8));
I don't quite understand why? why he XOR'ed to get in the next byte into remainder?
()withTOPBIT (1 << 7)to avoid unexpected code - although not an issue here.TOPBITdefine (as @chux wrote above) indicate that you changed the original code from the article. Do not do this if you don't understand what you're doing. Also, if you are not too tight with RAM, you might want to check the lookup table CRC version shown at the bottom of the article.