手算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
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 }
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 }
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 }
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 }
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 }
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 }
200190727更新
自己写了一个CRC表生成的小玩具,功能还比较少,日后慢慢拓展,放在了github上
https://github.com/constructorvirgil/app
平常很少用git,所以有些不规范,见谅