CRC8查表法中表的具体计算过程

来源:互联网 发布:杭州java培训有用 编辑:程序博客网 时间:2024/05/22 18:25

最近看CRC比较多,在翻译完一个英语文档后感觉对CRC的原理有了比较深入的了解,在理解原理后,进入CRC算法的实际应用,当我在网上查找CRC8资料时,看见最多的是这段代码:

/*****************************************************     描    述:  CRC8校验子程序 x^8+x^5+x^4+x^0     入口参数:  指向数组指针,校验字节个数     出口参数:  8位CRC校验码 ******************************************************/    const char CRC8Table[]={    0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,    157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,    35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,    190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,    70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,    219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,    101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,    248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,    140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,    17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,    175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,    50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,    202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,    87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,    233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,    116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53  };      unsigned char CRC8_Table(unsigned char *p, char counter)  {      unsigned char crc8 = 0;        for( ; counter > 0; counter--){          crc8 = CRC8Table[crc8^*p];          p++;      }      return(crc8);    }    /*****************************************************     描    述:  text ******************************************************/  void main()  {      unsigned  char  a[] = {0x02, 0x03};        printf("%d", CRC8_Table(&a, 2));        getch();  }
懂得CRC8计算原理的人都懂这个代码的意思,就是简单的查表异或还有取值。

这个应用的确很简单,但是面对前面那个庞大的数组,我很想知道它们是怎么被计算出来的~~~

如代码注释所说,这个CRC8的多项式是x^8+x^5+x^4+x^0

如果以后想换一个多项式时要怎么办,这就需要我要理解表的计算原理了。


我以表下标为0x01作为示例来进行计算:

首先需要明确CRC8常见的表格数据中是按照先传输LSB,并通过右移寄存器来判断的,因此每次要判断的就是寄存器的最低位LSB。同时要将多项式x^8+x^5+x^4+x^0(也就是0x131(0011 0001))按位颠倒后得到的0x8c(1000 1100)来在计算过程中做异或运算(为什么0x131中的第一个1没有被颠倒???!! 答:因为它是隐藏的。)。


具体步骤如下:

1 选择范围0~255(包含255)范围内的数据,设为temp

2 判断temp的最低位是否为1,如果为1,先将temp右移一位,然后将temp和多项式(0x8c)进行异或运算,结果保存在temp中。如果最低位为0,则将temp右移一位。

3 重复第二步,直到temp中的8位数据全部从右移出,然后进入4步

4 此时temp的值就是CRC8校验值


下面以0x01为例进行具体推导:

                             最低位      操作    总移位次数

        0000 0001        1            >>1       1

        0000 0000                            

xor   1000 1100 

____________

        1000 1100         0           >>1       2

        0100 0110         0           >>1       3

        0010 0011         1           >>1       4

        0001 0001 

xor 1000 1100

____________

        1001 1101          1            >>1     5

        0100 1110

xor   1000 1100

____________

        1100 0010          0           >>1      6

        0110 0001          1           >>1      7

        0011 0000              

xor 1000 1100              

____________

        1011 1100           0           >>1     8

        0101 1110           0x5e(H) 94(D)   这个值就是校验值


通过以上推导,表格中剩下的都是一样的计算过程。

下面贴出具体的CRC表格计算过程的C代码:

/*poly_src     = 1 0011 0001(B)poly         =   1000 1100(B)TABLE SIZE = 256(D)*/#include<stdio.h>unsigned int 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 main(void){int i,j,k = 0;short temp = 0x00;int poly_src = 0x31;//1 0011 0001(B)  the top 1 is hiddenint 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 bitif(temp){table[i] = table[i] >> 1;table[i] ^= poly;}else{table[i] = table[i] >> 1;}}}//end of calculating the CRC table    res = CRC8_Table(data, 2);    printf("res=0x%x\n",res);while(1);}
这个程序里面先计算了CRC8的表格内容,然后计算了一个示例,最后输出的结果为

res=0x35


以上是我对CRC8表格计算的理解,如果有什么问题或建议,还望能指出~

2 0
原创粉丝点击