Lets say I have an array with 8 bytes stored and I want compute the CRC code for that array(I mean all the elements inside it)
After some google search I found this polynomial x^8 + x^2 + x^1 + x^0
My question is how can I implement this
what is the value of "x" in the polynomial? Is the byte number?
if my case was:
I cannot remember where I got this, but this in an example of one that works in the arduino environment.
I modified it because I wanted to calculate the crc a byte at a time (it was for an application where data
was being read off a serial line, and I did not want to waste the buffer space).
Perhaps you can modify it for crc8.
This was in an include file crc16.h
/**************************************************************************
//
// crc16.c - generate a ccitt 16 bit cyclic redundancy check (crc)
/
// The code in this module generates the crc for a block of data.
//
**************************************************************************/
/*
// 16 12 5
// The CCITT CRC 16 polynomial is X + X + X + 1.
// In binary, this is the bit pattern 1 0001 0000 0010 0001, and in hex it
// is 0x11021.
// A 17 bit register is simulated by testing the MSB before shifting
// the data, which affords us the luxury of specifiy the polynomial as a
// 16 bit value, 0x1021.
// Due to the way in which we process the CRC, the bits of the polynomial
// are stored in reverse order. This makes the polynomial 0x8408.
*/
/* RH: modified original routine to a class called one char at a time */
#define POLY 0x8408
/*
// note: when the crc is included in the message, the valid crc is:
// 0xF0B8, before the complement and byte swap,
// 0x0F47, after complement, before the byte swap,
// 0x470F, after the complement and the byte swap.
*/
class Calc_Crc16 {
private:
unsigned int data;
unsigned int crc;
public:
const static short crc_ok = 0X470F;
Calc_Crc16() : crc ( 0XFFFF)
{
}
void reset()
{
crc = 0XFFFF;
}
void next (char dval)
{
int i;
for (i = 0, data = (unsigned int)0xff & dval;
i < 8;
i++, data >>= 1) {
if ((crc & 0x0001) ^ (data & 0x0001))
{ crc = (crc >> 1) ^ POLY;}
else
{ crc >>= 1;}
}
}
// taking copies of variables so this can be called validly more than once
short get_crc() {
short crc1 = ~crc;
short data1 = crc1;
crc1 = (crc1 << 8) | (data1 >> 8 & 0xFF);
return crc1;
}
}
;
This code snippet shows how to include, declare and use the routine above: