手算CRC及其实现

前言:

这篇文章主要讲的是如何手算CRC以及运用CRC,更侧重方法的步骤,对原理方面不做探讨。

 

方法也是按照我个人理解的来,所以并不专业~

一些搬过来的代码我也修改了一下下

 

如果想了解原理的可参考资料:

https://www.sogou.com/link?url=DSOYnZeCC_rx9YqZWdpzLQZDeOEOQWnQiCUwTUcmHnw3OuZTm80uIeGdxK9hd4ia

以及一些我参考过的博客:

https://blog.csdn.net/sinat_23338865/article/details/73549076

https://blog.csdn.net/xing414736597/article/details/78693781

在线检验CRC网站

http://www.ip33.com/crc.html

1.异或运算:

CRC运算需要不断地做异或运算,所以,如果要手算CRC,需要对异或运算十分熟悉。

写在前面相当于先热个身。

0 xor 1 = 1 . 1 xor 0 = 1 . 0 xor 0 = 0 . 1 xor 1= 0 .

简单的口诀:相同为1,不同为0

2.CRC标准的含义

更正:CRC-8实际上是CRC-8/MAXIM   有输入翻转和输出翻转(但本篇文章并没有实现),这里只是借用了一下生成多项式。

CRC-8       x8+x5+x4+1              0x31(0011 0001)

x^8 + (x^7) +(x^6) +x^5+x^4+(x^3)+(x^2)+(x)+1

带括号的表示系数为0的项(这些项会被去掉)

这里再看右边的0x31,这里是去掉最高次项x^8之后,多项式的矩阵的表达。

总结一下:

CRC-n 就是最高项为x^n,去掉x^n后 的矩阵表达 就是它的生成多项式

注意:

手算的时候,最高项保留

写程序的时候,最高项不保留(优化执行次数)

3.如何手算(一个字节)

测试数据:0x11

生成多项式:CRC-8 1 0011 0001

4.代码实现(分为4个level)

写程序的时候,最高项不保留(优化执行次数),下面的程序会体现

  • level1 计算一个字节

 1 //CRC-8
 2 //计算单个字节
 3 #include <iostream> //支持uint类型
 4 uint8_t cal_table_high_first(uint8_t value)
 5 {
 6     uint8_t crc;
 7     crc = value;
 8     /* 数据往左移了8位,需要计算8次 */
 9     for (int i =0 ; i< 8 ; i++)
10     {
11         //1000 0000 = 0x80
12         //判断最高位是否为1
13         if (crc & 0x80)
14         {
15           //如果为1,先左移一位然后再与多项式x31异或
16           //0011 0001 = 0x31 x^8+x^5+x^4+1
17           //左移一位是为了简化最高项x^8的计算
18              crc = (crc << 1) ^ 0x31;        }
19         else
20         {
21           //如果为0,则不需要异或,整体数据左移一位
22              crc = (crc << 1);
23         }
24     }
25     return crc;
26 }

 

 

 

  • level2 计算多个字节

把<上一个字节的CRC结果> 与 <下一个字节> 异或,得到的结果再进行CRC运算,那么就能得出两个字节的CRC检验码,然后以此类推,任意字节的数都可以计算了。

 1 //CRC-8
 2 //计算多个字节
 3 #include <iostream>  //支持uint类型
 4 uint8_t crc_high_first(uint8_t  *ptr, int len)
 5 {
 6     uint8_t crc=0x00/* 计算的初始crc值 */ 
 7     while(len--)
 8     {
 9         //先把上一字节与下一字节异或
10         crc ^= *ptr++;
11         //下面的代码与计算一个字节的一致
12         for (int i=0; i<8 ; i++) 
13         { 
14             if (crc & 0x80)
15                 crc = (crc << 1) ^ 0x31;
16             else
17                 crc = (crc << 1);
18         }
19     }
20     return (crc); 
21 }

 

 

 

  • level3 产生表

 1 //生成表
 2 //调用了上方的函数
 3 
 4 
 5 #include <iostream>  //支持uint类型
 6 #include <stdio.h>
 7 void  create_crc_table(void)
 8 {
 9 
10         int i = 0x00;
11 
12 
13         for (; i<=0xFF; i++)
14         {
15             if (0 == (i%16))
16                 printf("\n");
17 
18 
19             //把每个字节的CRC检验码的结果保存下来
20             printf("0x%.2x, ", cal_table_high_first (i));
21         }
22 }

 

 

  • level4 查表方式生成CRC

 1 //查表计算CRC
 2 #include <iostream>  //支持uint类型
 3 uint8_t cal_crc_table(uint8_t *ptr, int len) 
 4 {
 5     //初始化
 6     uint8_t crc = 0x00;
 7     while (len--)
 8     {
 9         crc = crc_table[crc ^ *ptr++];
10         //等价与  crc = crc_table[  crc ^ (*ptr) ];
11         //         ptr++;
12     }
13     return crc;
14 }

 

 

 

5. 其他

上面的CRC-8是实现比较简单的一种,

其他的CRC的标准有部分会要求初始值,输入翻转,输出翻转,以及异或输出等要求,所以手算其他CRC的时候要注意一下,不要对错答案了(不要问我为什么)

 

下面贴一个CRC-16/X25的实用代码,也是经过转载然后修改的来.

下面这段代码某有些问题,具体是

43行代码这里:

43         crc_reg = (crc_reg >> 8) ^ crc16_table[crc_reg ^ *data++];
改成
crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *data++)&0x00FF];
取一个低位,不然运算结果会出错
 1 #include <iostream>
 2 using namespace std;
 3 static unsigned short crc16_table[256] = {
 4     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
 5     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
 6     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
 7     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
 8     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
 9     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
10     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
11     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
12     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
13     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
14     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
15     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
16     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
17     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
18     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
19     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
20     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
21     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
22     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
23     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
24     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
25     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
26     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
27     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
28     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
29     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
30     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
31     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
32     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
33     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
34     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
35     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
36 };
37 
38 uint16_t crc_check(uint8_t *data, uint32_t length)
39 {
40     uint16_t crc_reg = 0xFFFF;
41     while (length--)
42     {
43         crc_reg = (crc_reg >> 8) ^ crc16_table[crc_reg ^ *data++];
44     }
45     return (uint16_t)(~crc_reg);
46 }
47 //先测试8个字节
48 #define BYTES 8
49 int main()
50 {
51     //用来测试的数据
52     uint8_t data[BYTES] = { 0x11,0x46,0x88,0x52,0x11,0x46,0x55,0x52 };
53     uint16_t res = crc_check(data, BYTES);
54     //输出CRC检验码
55     cout << std::hex << std::showbase << res << '\n';
56     return 0;
57 }

20190716 更新

增加一个标准的CRC-8可直接运行实例代码,方便测试。

 1 #include <iostream>
 2 using namespace std;
 3 //测试数据
 4 const uint8_t bytes_test[8] = { 0x11,0x23,0x4a,0x25,0xe4,0x65,0x89,0x99 };
 5 const uint8_t byte_test = 0x22;
 6 //CRC表
 7 uint8_t crc_table[256];
 8 
 9 uint8_t cal_crc_perbyte(uint8_t data)
10 {
11     uint8_t crc = data;
12 
13     for (int i = 0; i < 8; i++)
14     {
15         if (crc & 0x80)
16         {
17             crc = (crc << 1) ^ 0x07;
18         }
19         else
20         {
21             crc = crc << 1;
22         }
23     }
24     return crc;
25 }
26 uint8_t cal_crc_bytes(const uint8_t * data, int len)
27 {
28     uint8_t crc = 0x00;
29 
30     for (int i = 0; i < len; i++)
31     {
32         crc = crc^data[i];
33         crc = cal_crc_perbyte(crc);
34     }
35 
36     return crc;
37 }
38 
39 void gen_crc_table(uint8_t * table)
40 {
41     for (int i = 0; i <= 0xFF; i++)
42     {
43         table[i] = cal_crc_perbyte(i);
44     }
45 }
46 void print_crc_table(uint8_t * table,int len)
47 {
48     cout << "Table:";
49     //空白字符用0填充
50     cout.fill('0');
51     for (int i = 0; i < len; i++)
52     {
53         if (i % 10 == 0)cout << endl;
54         //输出控制为:
55         //0x00 0x01 0x02 类似的格式
56         cout << "0x";
57         cout.width(2);
58         cout << hex << (uint16_t)table[i] <<"  ";
59     }
60     cout << endl;
61 }
62 
63 uint8_t cal_crc_bytable(const uint8_t * data, int len)
64 {
65     uint8_t crc = 0x00;
66 
67     for (int i = 0; i < len; i++)
68     {
69         crc = crc_table[crc^data[i]];
70     }
71     return crc;
72 }
73 
74 
75 int main()
76 {
77     uint8_t res;
78     res = cal_crc_perbyte(byte_test);
79     cout << "cal_crc_perbyte" << endl;
80     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
81 
82     res = cal_crc_bytes(bytes_test,sizeof(bytes_test));
83     cout << "cal_crc_bytes" << endl;
84     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
85 
86     gen_crc_table(crc_table);
87     print_crc_table(crc_table,sizeof(crc_table));
88 
89     res = cal_crc_bytable(bytes_test, sizeof(bytes_test));
90     cout << "cal_crc_bytable" << endl;
91     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
92 
93     return 0;
94 }

 20190719更新

添加一个  CRC-8/MAXIM 实现代码,CRC-8/MAXIM 只比 标准CRC-8多了一个输入翻转,可以直接从标准CRC-8的代码来修改, 

 1 #include <iostream>
 2 using namespace std;
 3 //测试数据
 4 const uint8_t bytes_test[8] = { 0x11,0x23,0x4a,0x25,0xe4,0x65,0x89,0x99 };
 5 const uint8_t byte_test = 0x22;
 6 //CRC表
 7 uint8_t crc_table[256];
 8 
 9 uint8_t cal_crc_perbyte(uint8_t data)
10 {
11     uint8_t crc = data;
12 
13     for (int i = 0; i < 8 ; i++ )
14     {
15         if (crc & 0x01)
16         {
17             //由原来的 1 0011 0001(0x31) 翻转成  1 1000 1100(0x8C)
18             //注意最高位的1是不加入翻转过程的
19             crc = (crc >> 1) ^ 0x8C;
20         }
21         else
22         {
23             crc = crc >> 1;
24         }
25     }
26     return crc;
27 }
28 uint8_t cal_crc_bytes(const uint8_t * data, int len)
29 {
30     uint8_t crc = 0x00;
31 
32     //字节层面上的不用修改顺序,因为倒过来是一样的
33     for (int i = 0; i < len ; i++ )
34     {
35         crc = crc ^ data[i];
36         crc = cal_crc_perbyte(crc);
37     }
38 
39     return crc;
40 }
41 
42 void gen_crc_table(uint8_t * table)
43 {
44     for (int i = 0; i <= 0xFF; i++)
45     {
46         table[i] = cal_crc_perbyte(i);
47     }
48 }
49 void print_crc_table(uint8_t * table, int len)
50 {
51     cout << "Table:";
52     //空白字符用0填充
53     cout.fill('0');
54     for (int i = 0; i < len; i++)
55     {
56         if (i % 10 == 0)cout << endl;
57         //输出控制为:
58         //0x00 0x01 0x02 类似的格式
59         cout << "0x";
60         cout.width(2);
61         cout << hex << (uint16_t)table[i] << "  ";
62     }
63     cout << endl;
64 }
65 
66 uint8_t cal_crc_bytable(const uint8_t * data, int len)
67 {
68     uint8_t crc = 0x00;
69 
70     for (int i = 0; i < len; i++)
71     {
72         crc = crc_table[crc^data[i]];
73     }
74     return crc;
75 }
76 
77 
78 int main()
79 {
80     uint8_t res;
81     res = cal_crc_perbyte(byte_test);
82     cout << "cal_crc_perbyte" << endl;
83     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
84 
85     res = cal_crc_bytes(bytes_test, sizeof(bytes_test));
86     cout << "cal_crc_bytes" << endl;
87     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
88 
89     gen_crc_table(crc_table);
90     print_crc_table(crc_table, sizeof(crc_table));
91 
92     res = cal_crc_bytable(bytes_test, sizeof(bytes_test));
93     cout << "cal_crc_bytable" << endl;
94     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
95 
96     return 0;
97 }

再贴上CRC-8/ROHC,这里又比 CRC-8/MAXIM  多了一个初始值0xFF,  此外 , 生成多项式也不一样了,生成多项式为0x07(翻转后为0xE0)

防止篇幅过长,这里的代码折叠了

 1 #include <iostream>
 2 using namespace std;
 3 //测试数据
 4 const uint8_t bytes_test[8] = { 0x11,0x23,0x4a,0x25,0xe4,0x65,0x89,0x99 };
 5 const uint8_t byte_test = 0x22;
 6 //CRC表
 7 uint8_t crc_table[256];
 8 
 9 uint8_t cal_crc_perbyte(uint8_t data)
10 {
11     uint8_t crc = data;
12 
13     for (int i = 0; i < 8; i++)
14     {
15         if (crc & 0x01)
16         {
17             //由原来的 1 0000 0111‬(0x07) 翻转成  1 1110 0000‬(0xE0)
18             //注意最高位的1是不加入翻转过程的
19             crc = (crc >> 1) ^ 0xE0;
20         }
21         else
22         {
23             crc = crc >> 1;
24         }
25     }
26     return crc;
27 }
28 uint8_t cal_crc_bytes(const uint8_t * data, int len)
29 {
30     //初始值不再是0x00
31     uint8_t crc = 0xFF;
32 
33     //字节层面上的不用修改顺序,因为倒过来是一样的
34     for (int i = 0; i < 8; i++)
35     {
36         crc = crc ^ data[i];
37         crc = cal_crc_perbyte(crc);
38     }
39 
40     return crc;
41 }
42 
43 void gen_crc_table(uint8_t * table)
44 {
45     for (int i = 0; i <= 0xFF; i++)
46     {
47         table[i] = cal_crc_perbyte(i);
48     }
49 }
50 void print_crc_table(uint8_t * table, int len)
51 {
52     cout << "Table:";
53     //空白字符用0填充
54     cout.fill('0');
55     for (int i = 0; i < len; i++)
56     {
57         if (i % 10 == 0)cout << endl;
58         //输出控制为:
59         //0x00 0x01 0x02 类似的格式
60         cout << "0x";
61         cout.width(2);
62         cout << hex << (uint16_t)table[i] << "  ";
63     }
64     cout << endl;
65 }
66 
67 uint8_t cal_crc_bytable(const uint8_t * data, int len)
68 {
69     //初始值不再是0x00
70     uint8_t crc = 0xFF;
71 
72     for (int i = 0; i < len; i++)
73     {
74         crc = crc_table[crc^data[i]];
75     }
76     return crc;
77 }
78 
79 
80 int main()
81 {
82     uint8_t res;
83     res = cal_crc_perbyte(byte_test);
84     cout << "cal_crc_perbyte" << endl;
85     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
86 
87     res = cal_crc_bytes(bytes_test, sizeof(bytes_test));
88     cout << "cal_crc_bytes" << endl;
89     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
90 
91     gen_crc_table(crc_table);
92     print_crc_table(crc_table, sizeof(crc_table));
93 
94     res = cal_crc_bytable(bytes_test, sizeof(bytes_test));
95     cout << "cal_crc_bytable" << endl;
96     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
97 
98     return 0;
99 }
View Code

 

20190726更新:

前面的代码有一点BUG,创建表的时候缺少了0xFF的值没有创建,现在已经更改

再贴上CRC16/X25的生成表的方法(前面已经有现成的查表方法了,为了加深理解,再贴上生成表的代码)

  1 #include <iostream>
  2 using namespace std;
  3 //测试数据
  4 const uint8_t bytes_test[8] = { 0x11,0x23,0x4a,0x25,0xe4,0x65,0x89,0x99 };
  5 const uint8_t byte_test = 0x22;
  6 //CRC表
  7 uint16_t crc_table[256];
  8 
  9 //为了生成表,函数内部忽略了初始值和结果异或值的计算
 10 uint16_t cal_crc_perbyte(uint8_t data)
 11 {
 12     uint16_t crc = data;
 13     for (int i = 0; i < 8; i++)
 14     {
 15         if (crc & 0x01)
 16         {
 17             //0001 0000 0010 0001‬   0x1021
 18             //翻转后 1000 0100 0000 0001  0x8408
 19             crc = (crc >> 1) ^ (0x8408);
 20         }
 21         else
 22         {
 23             crc = crc >> 1;
 24         }
 25     }
 26     return crc;
 27 }
 28 uint16_t cal_crc_bytes(const uint8_t * data, int len)
 29 {
 30     //初始值
 31     uint16_t crc = 0xFFFF;
 32 
 33     for (int i = 0; i < len; i++)
 34     {
 35         crc = crc ^ data[i]; 
 36         crc = (  crc>>8  ) ^ cal_crc_perbyte(crc & 0xFF);
 37     }
 38     //结果异或值
 39     crc = ~crc;
 40 
 41     return crc;
 42 }
 43 
 44 void gen_crc_table(uint16_t * table)
 45 {
 46     for (int i = 0; i <= 0xFF; i++)
 47     {
 48         table[i] = cal_crc_perbyte(i);
 49     }
 50 }
 51 void print_crc_table(uint16_t * table, int len)
 52 {
 53     cout << "Table:";
 54     //空白字符用0填充
 55     cout.fill('0');
 56     for (int i = 0; i < len; i++)
 57     {
 58         if (i % 10 == 0)cout << endl;
 59         //输出控制为:
 60         //0x00 0x01 0x02 类似的格式
 61         cout << "0x";
 62         cout.width(4);
 63         cout << hex << (uint16_t)table[i] << "  ";
 64     }
 65     cout << endl;
 66 }
 67 
 68 uint16_t cal_crc_bytable(const uint8_t * data, int len)
 69 {
 70     //初始值
 71     uint16_t crc = 0xFFFF;
 72 
 73     for (int i = 0; i < len; i++)
 74     {
 75         crc = (crc>>8) ^ crc_table[  (crc^data[i]) & 0xFF ];
 76     }
 77     //异或值
 78     crc = ~crc;
 79     return crc;
 80 }
 81 
 82 
 83 int main()
 84 {
 85 
 86     cout.fill('0');
 87     uint16_t res;
 88     res = cal_crc_perbyte(byte_test);
 89 
 90     cout << "cal_crc_perbyte" << endl;
 91     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
 92 
 93     res = cal_crc_bytes(bytes_test, sizeof(bytes_test));
 94     cout << "cal_crc_bytes" << endl;
 95     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
 96 
 97     gen_crc_table(crc_table);
 98     print_crc_table(crc_table, sizeof(crc_table) / 2);//是字节数,所以要除以2
 99 
100     res = cal_crc_bytable(bytes_test, sizeof(bytes_test));
101     cout << "cal_crc_bytable" << endl;
102     cout << "0x" << hex << uppercase << (uint16_t)res << endl;
103 
104     return 0;
105 }
View Code

 200190727更新

自己写了一个CRC表生成的小玩具,功能还比较少,日后慢慢拓展,放在了github上

https://github.com/constructorvirgil/app

平常很少用git,所以有些不规范,见谅

posted @ 2019-07-15 21:34  virgil_devil  阅读(2718)  评论(0编辑  收藏  举报