CRC

来源:互联网 发布:手机dns优化apk 编辑:程序博客网 时间:2024/05/15 16:41

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

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

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2. poly_src     = 1 0011 0001(B) 
  3. poly         =   1000 1100(B) 
  4. TABLE SIZE = 256(D) 
  5. */  
  6. #include<stdio.h>  
  7.   
  8. unsigned int table[256] = {0};  
  9.   
  10. unsigned char CRC8_Table(unsigned char *p, char counter)  
  11. {  
  12.     unsigned char res = 0x00;  
  13.     for(;counter >0 ; counter--){  
  14.         res = table[res ^ *p];  
  15.         p++;  
  16.     }  
  17.     return res;  
  18. }  
  19.   
  20. void main(void)  
  21. {  
  22.     int i,j,k = 0;  
  23.     short temp = 0x00;  
  24.     int poly_src = 0x31;//1 0011 0001(B)  the top 1 is hidden  
  25.     int poly     = 0x8c;//  1000 1100(B)  
  26.     unsigned short reg = 0x3e;  
  27.   
  28.     unsigned char data[2] = {0x02,0x43};  
  29.     unsigned char res = 0x00;  
  30.   
  31. //start of calculating the CRC8 table  
  32.     for(i = 0;i < 256; i++){  
  33.         table[i] = i;  
  34.     }  
  35.   
  36.     for(i = 0;i < 256; i++){  
  37.         for(j = 7;j >= 0; j--){  
  38.             temp = table[i] & 0x01;//take the last bit  
  39.             if(temp){  
  40.                 table[i] = table[i] >> 1;  
  41.                 table[i] ^= poly;  
  42.             }  
  43.             else{  
  44.                 table[i] = table[i] >> 1;  
  45.             }  
  46.         }  
  47.     }  
  48. //end of calculating the CRC table  
  49.   
  50.     res = CRC8_Table(data, 2);  
  51.   
  52.     printf("res=0x%x\n",res);  
  53.   
  54.     while(1);  
  55. }  
这个程序里面先计算了CRC8的表格内容,然后计算了一个示例,最后输出的结果为

res=0x35

0 0