CRC8算法表有两种形式,第一种是256个字节的表,第二种是高4位16字节,低4位16字节。

根据不同多项式算出对应的表中的数据

第一种方法:多项式( 100110001----0x131)

#include <iostream>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 bitif(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 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 bit//       if(temp){//             table[i] = table[i] >> 1;//          table[i] ^= poly;//        }//         else{//             table[i] = table[i] >> 1;//      }//     }// }
//end of calculating the CRC tablecrc_table_create(table, poly);for(i = 0;i < 256; i++){cout<<table[i]<<endl;}res = CRC8_Table(data, 2);printf("res=0x%x\n",res);while(1);
}

计算出的表格是:

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
}; 

第二种方法:多项式( 00101111----0x2F)

#include <stdio.h>unsigned char crc_table_create(unsigned char tableIndex, unsigned char factor)
{unsigned char i; unsigned char crc; /* 计算的初始crc值 */ crc = tableIndex^0xFF;  /* 每次先与需要计算的数据异或,计算完指向下一数据 */  for (i=8; i>0; --i)   /* 下面这段计算过程与计算一个字节crc一样 */  { if (crc & 0x80)crc = (crc << 1) ^ factor;elsecrc = (crc << 1);}return (crc);
}int main(void)
{unsigned char crc_tab[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};unsigned char crc_tab1[16] = {0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0};unsigned char crc_res[16];unsigned char crc_res1[16];for(int i=0; i<16; i++){crc_res[i] = crc_table_create(crc_tab[i], 0x2F);printf("crc_res[%d] = 0x%x\n",i, crc_res[i]);}for(int i=0; i<16; i++){crc_res1[i] = crc_table_create(crc_tab1[i], 0x2F);printf("crc_res1[%d] = 0x%x\n",i, crc_res1[i]);}}

算出的表格为:

/* Constant array CRC8 */static const uint8 LIB_Crc8Table1[16]={0x42u, 0x6du, 0x1cu, 0x33u, 0xfeu, 0xd1u, 0xa0u, 0x8fu,0x15u, 0x3au, 0x4bu, 0x64u, 0xa9u, 0x86u, 0xf7u, 0xd8u};static const uint8 LIB_Crc8Table2[16]={0x42u, 0xecu, 0x31u, 0x9fu, 0xa4u, 0x0au, 0xd7u, 0x79u,0xa1u, 0x0fu, 0xd2u, 0x7cu, 0x47u, 0xe9u, 0x34u, 0x9au};

CRC8算法

/*** @brief This function calculates a CRC8 over the data buffer* @param LIB_TempInputCrc8_cp[in]: pointer to the input data* @param LIB_TempLengthCrc8_u16[in]: Length of the input data* @return Calculated CRC8* @details Local variables*          Loop over all byte*              Execute CRC algorithm*          Return inverted result* @ReqKey MOD_LIB-64, MOD_LIB-65*/
uint8 LIB_Crc8(const uint8 *LIB_TempInputCrc8_cp, const uint16 LIB_TempLengthCrc8_u16)
{/* Constant array CRC8 */static const uint8 LIB_Crc8Table1[16]={0x42u, 0x6du, 0x1cu, 0x33u, 0xfeu, 0xd1u, 0xa0u, 0x8fu,0x15u, 0x3au, 0x4bu, 0x64u, 0xa9u, 0x86u, 0xf7u, 0xd8u};static const uint8 LIB_Crc8Table2[16]={0x42u, 0xecu, 0x31u, 0x9fu, 0xa4u, 0x0au, 0xd7u, 0x79u,0xa1u, 0x0fu, 0xd2u, 0x7cu, 0x47u, 0xe9u, 0x34u, 0x9au};/* Local Variables */uint8 LIB_TempCrc8_u8 = 0xFFu;uint16 LIB_TempIndexCrc8_u16;/* Loop over all bytes */for (LIB_TempIndexCrc8_u16 = 0u; LIB_TempIndexCrc8_u16 < LIB_TempLengthCrc8_u16; LIB_TempIndexCrc8_u16++){/* CRC Algorithm */LIB_TempCrc8_u8 = LIB_TempInputCrc8_cp[LIB_TempIndexCrc8_u16] ^ LIB_TempCrc8_u8;LIB_TempCrc8_u8 = (LIB_Crc8Table1 [LIB_TempCrc8_u8 & 0x0Fu]) ^ (LIB_Crc8Table2 [LIB_TempCrc8_u8 >> 4u]);}return (LIB_TempCrc8_u8^0xFF);}

CRC-8 校验算法的表计算相关推荐

  1. CRC32校验算法(查表法)

    CRC算法参数 CRC result width: 32 bits Polynomial: 04C11DB7h Initial value: FFFFFFFFh Input data reflecte ...

  2. 【转载】CRC32校验算法C语言版(查表法)

    先放原文链接:CRC32校验算法C语言版(查表法) 这几天搞串口通信,用到CRC32,把以前用到的东西整理一下,方便以后使用. STM32F103 芯片自带的CRC32硬件算法,匹配上位机CRC32算 ...

  3. crc16检验 python_Python CRC16校验算法

    def crc16(x, invert): a = 0xFFFF b = 0xA001 for byte in x: a ^= ord(byte) for i in range(8): last = ...

  4. CRC查表法——表的由来及Java实现CRC8校验算法

    转载请标明出处: http://blog.csdn.net/xx326664162/article/details/51718857 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定 ...

  5. CRC校验算法的数学原理(上)

    介绍   CRC是Cyclic Redundancy Check的缩写,用中文来讲,就是 循环冗余校验.是一种通过对数据产生固定位数校验码以备侦测数据错误的数据校验技术,主要用来侦测数据传输错误,也可 ...

  6. CRC-16原理及通用的16位CRC校验算法代码

    CRC-16原理及通用的16位CRC校验算法代码 循环冗余码校验英文名称为Cyclical Redundancy Check,简称CRC.它是利用除法及余数的原理来作错误侦测(Error Detect ...

  7. 【基础知识】CRC(循环冗余校验)直接计算和查表法

    CRC概述 校验 校验是什么,个人理解就是经过一个算法,使用大量数据(几MB的数据)生成较小长度的一串信息(如16Bit),并切要做到 原数据不同时,生成的信息大概率不同(不是加密算法不考虑刻意造数据 ...

  8. 常用的简单校验算法:校验和,异或校验,crc校验,LRC校验,补码求和,checksum

    常用的简单校验算法:校验和,异或校验,crc校验,LRC校验,补码求和,checksum 相关思路和源码来自网络,自己只是整理, 做笔记用. 并未完整完善正确归纳,只是个人理解初步做笔记记录. 在实现 ...

  9. bcc校验位怎么算的_BCC(异或校验)、CRC、LRC校验算法

    一.校验算法 BCC(Block Check Character/信息组校验码),好像也是常说的异或校验方法 CRC(Cyclic Redundancy Check/循环冗余校验) LRC(Longi ...

最新文章

  1. 减少过敏反应的生活细节
  2. python如何读取excel数据-使用Python读取电子表格中的数据
  3. 最常用的15大Eclipse开发快捷键技巧
  4. undo表空间文件丢失恢复(3)--无备份无redo的情况下恢复
  5. js 遍历对象的几种方法
  6. DIY协同办公平台(C/S)系列4之通讯录篇
  7. 这九张动态图诠释什么是自然与科技的完美结合
  8. 解决webstorm out of memory内存不足问题
  9. 搭建基于hyperledger fabric的联盟社区(六) --搭建node.js服务器
  10. 计算机组成原理串行加法器延迟时间,2021考研408计算机组成原理:串行加法器和并行加法器...
  11. Mac上编译mingw
  12. 【异常】Reason: Executor heartbeat timed out after 140927 ms
  13. c++除法保留小数_小学数学整数和小数的应用题解答方法公式汇总,新学期必备...
  14. VMware故障:配置文件(.vmx)损坏修复
  15. SpringBoot 在main或者普通类中条用service接口
  16. Java操作Excel电子表格
  17. 老板怎么舍得你离开?-让我来教你如何拍马屁
  18. OSChina 周五乱弹 ——和我斗!要赢好难!
  19. 浙政钉h5微应用开发vue
  20. 装箱和拆箱,自动装箱和自动拆箱

热门文章

  1. Python文档基础操作
  2. vue 声明周期函数_Vue 生命周期详解
  3. qt creator release编译无错误,输出crashed解决方法
  4. String的charAt方法
  5. 微信营销二(微信营销的意义)
  6. 如何处理客户投诉的问题
  7. ArcGIS安装的关键步骤
  8. spring 之 ObjectPostProcessor
  9. 莫比乌斯反演入门讲解
  10. SystemUI介绍