CRC8算法表有两种形式,第一种是256个字节的表,第二种是高4位16字节,低4位16字节。
根据不同多项式算出对应的表中的数据
第一种方法:多项式 ( 100110001----0x131)
#include
using namespace std;
unsigned char table[256] = {0};
unsigned char CRC8_Table(unsigned char *p, char counter)
{
unsigned char res = 0x00;
for(;counter >0 ; counter--)
{
res = table[res ^ *p];
p++;
}
return res;
}
//反序
void crc_table_create(unsigned char *pdata, unsigned char factor)
{
unsigned char temp;
for(int i = 0;i < 256; i++)
{
pdata[i] = i;
}
for(int i = 0;i < 256; i++)
{
for(int j = 7;j >= 0; j--)
{
temp = pdata[i] & 0x01;//take the last bit
if(temp) //the last bit is 1
{
pdata[i] = pdata[i] >> 1;
pdata[i] ^= factor;
}
else
{
pdata[i] = pdata[i] >> 1;
}
}
}
}
int main(void)
{
int i,j,k = 0;
short temp = 0x00;
int poly_src = 0x31;//1 0011 0001(B) the top 1 is hidden
int poly = 0x8c;// 1000 1100(B)
unsigned short reg = 0x3e;
unsigned char data[2] = {0x02,0x43};
unsigned char res = 0x00;
//start of calculating the CRC8 table
// for(i = 0;i < 256; i++){
// table[i] = i;
// }
// for(i = 0;i < 256; i++){
// for(j = 7;j >= 0; j--){
// temp = table[i] & 0x01;//take the last bit
// if(temp){
// table[i] = table[i] >> 1;
// table[i] ^= poly;
// }
// else{
// table[i] = table[i] >> 1;
// }
// }
// }
//end of calculating the CRC table
crc_table_create(table, poly);
for(i = 0;i < 256; i++)
{
cout |