CRC16常见的标准

2024-01-09 23:44
文章标签 常见 标准 crc16

本文主要是介绍CRC16常见的标准,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文整理CRC16常见的标准算法,有以下几种:

模式多项式初始值数据位序结果处理
CRC16_CCITTx16+x12+x5+1(0x1021)0x0000低位在前,高位在后与0x0000异或
CRC16_CCITT_FALSEx16+x12+x5+1(0x1021)0xFFFF低位在后,高位在前与0x0000异或
CRC16_XMODEMx16+x12+x5+1(0x1021)0x0000低位在后,高位在前与0x0000异或
CRC16_X25x16+x12+x5+1(0x1021)0xFFFF低位在前,高位在后与0xFFFF异或
CRC16_ MODBUSx16+x15+x2+1(0x8005)0xFFFF低位在前,高位在后与0x0000异或
CRC16_ IBMx16+x15+x2+1(0x8005)0x0000低位在前,高位在后与0x0000异或
CRC16_ MAXIMx16+x15+x2+1(0x8005)0x0000低位在前,高位在后与0xFFFF异或
CRC16_ USBx16+x15+x2+1(0x8005)0xFFFF低位在前,高位在后与0xFFFF异或

多项式产生:
如x16+x12+x5+1
x16表示第16位为1,x5表示第5位为1
(1 << 16) | (1 << 12) | (1 << 5) | (1) = 0x11021
但是CRC16只取低16位,写成16进制数就是 0x1021

CRC16的算法原理:

1.根据CRC16的标准选择初值CRCIn的值。

2.将数据的第一个字节与CRCIn高8位异或。

3.判断最高位,若该位为 0 左移一位,若为 1 左移一位再与多项式Hex码异或。

4.重复3直至8位全部移位计算结束。

5.重复将所有输入数据操作完成以上步骤,所得16位数即16位CRC校验码。

根据算法原理与标准要求就能简单的写出具体程序:CRC算法参数模型解释: 
    NAME:参数模型名称。 
    WIDTH:宽度,即CRC比特数。 
    POLY:生成项的简写,以16进制表示。例如:CRC-32即是0x04C11DB7,忽略了最高位的"1",即完整的生成项是0x104C11DB7。 
    INIT:这是算法开始时寄存器(crc)的初始化预置值,十六进制表示。 
    REFIN:待测数据的每个字节是否按位反转,True或False。 
    REFOUT:在计算后之后,异或输出之前,整个数据是否按位反转,True或False。 
    XOROUT:计算结果与此参数异或后得到最终的CRC值。
    Alias:别名及其应用范围。

/****************************Info********************************************** * Name:    CRC-16/CCITT        x16+x12+x5+1 * Width:    16* Poly:    0x1021 * Init:    0x0000 * Refin:   True * Refout:  True * Xorout:  0x0000 * Alias:   CRC-CCITT,CRC-16/CCITT-TRUE,CRC-16/KERMIT *****************************************************************************/ 
#if 0
unsigned short CRC16_CCITT(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x1021;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin);
}
#else 
//这里为了效率,我们不需要将所有Refin和refout为true的输入输出数据移位转换
//只需要将poly二项式转换后,运算时将左移变为右移
unsigned short CRC16_CCITT(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x1021;unsigned char wChar = 0;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin);
}
#endif
/****************************Info********************************************** * Name:    CRC-16/CCITT-FALSE   x16+x12+x5+1 * Width:    16 * Poly:    0x1021 * Init:    0xFFFF * Refin:   False * Refout:  False * Xorout:  0x0000 * Note: *****************************************************************************/ 
unsigned short CRC16_CCITT_FALSE(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x1021;while (datalen--)     {wCRCin ^= *(data++) << 8;for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}return (wCRCin);
}
/****************************Info********************************************** * Name:    CRC-16/XMODEM       x16+x12+x5+1 * Width:    16 * Poly:    0x1021 * Init:    0x0000 * Refin:   False * Refout:  False * Xorout:  0x0000 * Alias:   CRC-16/ZMODEM,CRC-16/ACORN *****************************************************************************/ 
unsigned short CRC16_XMODEM(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x1021;while (datalen--)     {wCRCin ^= (*(data++) << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}return (wCRCin);
}
/****************************Info********************************************** * Name:    CRC-16/X25          x16+x12+x5+1 * Width:    16 * Poly:    0x1021 * Init:    0xFFFF * Refin:   True * Refout:  True * Xorout:  0XFFFF * Note: *****************************************************************************/
#if 0 
unsigned short CRC16_X25(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x1021;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin^0xFFFF);
}
#else 
unsigned short CRC16_X25(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x1021;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin^0xFFFF);
}
#endif
/****************************Info********************************************** * Name:    CRC-16/MODBUS       x16+x15+x2+1 * Width:    16 * Poly:    0x8005 * Init:    0xFFFF * Refin:   True * Refout:  True * Xorout:  0x0000 * Note: *****************************************************************************/
#if 0 
unsigned short CRC16_MODBUS(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x8005;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin);
}
#else 
unsigned short CRC16_MODBUS(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x8005;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin);
}
#endif
/****************************Info********************************************** * Name:    CRC-16/IBM          x16+x15+x2+1 * Width:    16 * Poly:    0x8005 * Init:    0x0000 * Refin:   True * Refout:  True * Xorout:  0x0000 * Alias:   CRC-16,CRC-16/ARC,CRC-16/LHA *****************************************************************************/ 
#if 0
unsigned short CRC16_IBM(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x8005;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin);
}
#else 
unsigned short CRC16_IBM(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x8005;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin);
}
#endif
/****************************Info********************************************** * Name:    CRC-16/MAXIM        x16+x15+x2+1 * Width:    16 * Poly:    0x8005 * Init:    0x0000 * Refin:   True * Refout:  True * Xorout:  0xFFFF * Note: *****************************************************************************/
#if 0
unsigned short CRC16_MAXIM(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x8005;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin^0xFFFF);
}
#else 
unsigned short CRC16_MAXIM(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x8005;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin^0xFFFF);
}
#endif
/****************************Info********************************************** * Name:    CRC-16/USB          x16+x15+x2+1 * Width:    16 * Poly:    0x8005 * Init:    0xFFFF * Refin:   True * Refout:  True * Xorout:  0xFFFF * Note: *****************************************************************************/ 
#if 0
unsigned short CRC16_USB(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x8005;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin^0xFFFF);
}
#else 
unsigned short CRC16_USB(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0xFFFF;unsigned short wCPoly = 0x8005;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin^0xFFFF);
}
#endif
/****************************Info********************************************** * Name:    CRC-16/DNP          x16+x13+x12+x11+x10+x8+x6+x5+x2+1 * Width:    16 * Poly:    0x3D65 * Init:    0x0000 * Refin:   True * Refout:  True * Xorout:  0xFFFF * Use:     M-Bus,ect. *****************************************************************************/  
#if 0
unsigned short CRC16_DNP(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x3D65;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 8);for(int i = 0;i < 8;i++){if(wCRCin & 0x8000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint16(&wCRCin,&wCRCin);return (wCRCin^0xFFFF) ;
}
#else
unsigned short CRC16_DNP(unsigned char *data, unsigned int datalen)
{unsigned short wCRCin = 0x0000;unsigned short wCPoly = 0x3D65;InvertUint16(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = (wCRCin >> 1);}}return (wCRCin^0xFFFF);
}
#endif/****************************Info********************************************** * Name:    InvertUint8 * Note:     把字节颠倒过来,如0x12变成0x480x12: 0001 00100x48: 0100 1000*****************************************************************************/
void InvertUint8(unsigned char *dBuf,unsigned char *srcBuf)
{int i;unsigned char tmp[4]={0};for(i=0;i< 8;i++){if(srcBuf[0]& (1 << i))tmp[0]|=1<<(7-i);}dBuf[0] = tmp[0];}void InvertUint16(unsigned short *dBuf,unsigned short *srcBuf)
{int i;unsigned short tmp[4]={0};for(i=0;i< 16;i++){if(srcBuf[0]& (1 << i))tmp[0]|=1<<(15 - i);}dBuf[0] = tmp[0];
}void InvertUint32(unsigned int *dBuf,unsigned int *srcBuf)
{int i;unsigned int tmp[4]={0};for(i=0;i< 32;i++){if(srcBuf[0]& (1 << i))tmp[0]|=1<<(31 - i);}dBuf[0] = tmp[0];
}

在这个基础上也加入CRC32 的校验算法

/****************************Info********************************************** * Name:    CRC-32  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1 * Width:    32 * Poly:    0x4C11DB7 * Init:    0xFFFFFFF * Refin:   True * Refout:  True * Xorout:  0xFFFFFFF * Alias:   CRC_32/ADCCP * Use:     WinRAR,ect. *****************************************************************************/  
#if 0
unsigned int CRC32(unsigned char *data, unsigned int datalen)
{unsigned int wCRCin = 0xFFFFFFFF;unsigned int wCPoly = 0x04C11DB7;unsigned int wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8((unsigned char *)&wChar,(unsigned char *)&wChar);wCRCin ^= (wChar << 24);for(int i = 0;i < 8;i++){if(wCRCin & 0x80000000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint32(&wCRCin,&wCRCin);return (wCRCin ^ 0xFFFFFFFF) ;
}
#else
unsigned int CRC32(unsigned char *data, unsigned int datalen)
{unsigned int wCRCin = 0xFFFFFFFF;unsigned int wCPoly = 0x04C11DB7;InvertUint32(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin ^ 0xFFFFFFFF) ;
}
#endif
/****************************Info********************************************** * Name:    CRC-32/MPEG-2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1 * Width:    32 * Poly:    0x4C11DB7 * Init:    0xFFFFFFF * Refin:   False * Refout:  False * Xorout:  0x0000000 * Note: *****************************************************************************/ 
unsigned int CRC32_MPEG(unsigned char *data, unsigned int datalen)
{unsigned int wCRCin = 0xFFFFFFFF;unsigned int wCPoly = 0x04C11DB7;unsigned int wChar = 0;while (datalen--)     {wChar = *(data++);wCRCin ^= (wChar << 24);for(int i = 0;i < 8;i++){if(wCRCin & 0x80000000)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}return (wCRCin) ;
}
对于CRC32可能还有其他的多项式和初始值和结果值是否需要异或以及输入数据是否需要位序倒转等要求在源码中修改本次在CRC算法的基础上加入CRC-4,5,6,7,8的各个算法实现:/****************************Info********************************************** * Name:    CRC-4/ITU    x4+x+1 * Width:    4* Poly:    0x03 * Init:    0x00 * Refin:   True * Refout:  True * Xorout:  0x00 * Note: *****************************************************************************/
unsigned char CRC4_ITU(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x03;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ (wCPoly << 4);elsewCRCin = wCRCin << 1;}}InvertUint8(&wCRCin,&wCRCin);return (wCRCin);
}
/****************************Info********************************************** * Name:    CRC-5/EPC    x5+x3+1 * Width:    5* Poly:    0x09 * Init:    0x09 * Refin:   False * Refout:  False * Xorout:  0x00 * Note: *****************************************************************************/
unsigned char CRC5_EPC(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x09<<3;unsigned char wCPoly = 0x09<<3;while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ (wCPoly);elsewCRCin = wCRCin << 1;}}return (wCRCin >> 3);
}
/****************************Info********************************************** * Name:    CRC-5/USB    x5+x2+1 * Width:    5* Poly:    0x05 * Init:    0x1F * Refin:   True * Refout:  True * Xorout:  0x1F * Note: *****************************************************************************/
#if 0
unsigned char CRC5_USB(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x1F<<3;unsigned char wCPoly = 0x05;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ (wCPoly << 3);elsewCRCin = wCRCin << 1;}}InvertUint8(&wCRCin,&wCRCin);return (wCRCin^0x1F);
}
#else
unsigned char CRC5_USB(unsigned char *data, unsigned int datalen)  
{  unsigned char wCRCin = 0x1F;unsigned char wCPoly = 0x05;InvertUint8(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ (wCPoly >> 3);elsewCRCin = wCRCin >> 1;}}return (wCRCin^0x1F); 
} 
#endif
/****************************Info********************************************** * Name:    CRC-5/ITU    x5+x4+x2+1  * Width:    5* Poly:    0x15 * Init:    0x00 * Refin:   True * Refout:  True * Xorout:  0x00 * Note: *****************************************************************************/#if 0
unsigned char CRC5_ITU(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x15;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ (wCPoly << 3);elsewCRCin = wCRCin << 1;}}InvertUint8(&wCRCin,&wCRCin);return (wCRCin);
}
#else
unsigned char CRC5_ITU(unsigned char *data, unsigned int datalen)  
{  unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x15;InvertUint8(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ (wCPoly >> 3);elsewCRCin = wCRCin >> 1;}}return (wCRCin); 
} 
#endif
/****************************Info********************************************** * Name:    CRC-6/ITU    x6+x+1 * Width:    6* Poly:    0x03 * Init:    0x00 * Refin:   True * Refout:  True * Xorout:  0x00 * Note: *****************************************************************************/
unsigned char CRC6_ITU(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x03;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ (wCPoly << 2);elsewCRCin = wCRCin << 1;}}InvertUint8(&wCRCin,&wCRCin);return (wCRCin);
}
/****************************Info********************************************** * Name:    CRC-7/MMC           x7+x3+1  * Width:    7* Poly:    0x09 * Init:    0x00 * Refin:   False * Refout:  False * Xorout:  0x00 * Use:     MultiMediaCard,SD,ect. *****************************************************************************/
unsigned char CRC7_MMC(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x09;while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ (wCPoly<<1);elsewCRCin = wCRCin << 1;}}return (wCRCin>>1);
}
/****************************Info********************************************** * Name:    CRC-8               x8+x2+x+1 * Width:    8 * Poly:    0x07 * Init:    0x00 * Refin:   False * Refout:  False * Xorout:  0x00 * Note: *****************************************************************************/
unsigned char CRC8(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x07;while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}return (wCRCin);
}
/****************************Info********************************************** * Name:    CRC-8/ITU           x8+x2+x+1 * Width:    8 * Poly:    0x07 * Init:    0x00 * Refin:   False * Refout:  False * Xorout:  0x55 * Alias:   CRC-8/ATM *****************************************************************************/
unsigned char CRC8_ITU(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x07;while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}return (wCRCin^0x55);
}
/****************************Info********************************************** * Name:    CRC-8/ROHC          x8+x2+x+1 * Width:    8 * Poly:    0x07 * Init:    0xFF * Refin:   True * Refout:  True * Xorout:  0x00 * Note: *****************************************************************************/
#if 0
unsigned char CRC8_ROHC(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0xFF;unsigned char wCPoly = 0x07;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 0);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint8(&wCRCin,&wCRCin);return (wCRCin);
}
#else 
unsigned char CRC8_ROHC(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0xFF;unsigned char wCPoly = 0x07;InvertUint8(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin);
}
#endif
/****************************Info********************************************** * Name:    CRC-8/MAXIM         x8+x5+x4+1 * Width:    8 * Poly:    0x31 * Init:    0x00 * Refin:   True * Refout:  True * Xorout:  0x00 * Alias:   DOW-CRC,CRC-8/IBUTTON * Use:     Maxim(Dallas)'s some devices,e.g. DS18B20 *****************************************************************************/ 
#if 0
unsigned char CRC8_MAXIM(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x31;unsigned char wChar = 0;while (datalen--)     {wChar = *(data++);InvertUint8(&wChar,&wChar);wCRCin ^= (wChar << 0);for(int i = 0;i < 8;i++){if(wCRCin & 0x80)wCRCin = (wCRCin << 1) ^ wCPoly;elsewCRCin = wCRCin << 1;}}InvertUint8(&wCRCin,&wCRCin);return (wCRCin);
}
#else 
unsigned char CRC8_MAXIM(unsigned char *data, unsigned int datalen)
{unsigned char wCRCin = 0x00;unsigned char wCPoly = 0x31;InvertUint8(&wCPoly,&wCPoly);while (datalen--)     {wCRCin ^= *(data++);for(int i = 0;i < 8;i++){if(wCRCin & 0x01)wCRCin = (wCRCin >> 1) ^ wCPoly;elsewCRCin = wCRCin >> 1;}}return (wCRCin);
}
#endif

这篇关于CRC16常见的标准的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/588842

相关文章

数据治理框架-ISO数据治理标准

引言 "数据治理"并不是一个新的概念,国内外有很多组织专注于数据治理理论和实践的研究。目前国际上,主要的数据治理框架有ISO数据治理标准、GDI数据治理框架、DAMA数据治理管理框架等。 ISO数据治理标准 改标准阐述了数据治理的标准、基本原则和数据治理模型,是一套完整的数据治理方法论。 ISO/IEC 38505标准的数据治理方法论的核心内容如下: 数据治理的目标:促进组织高效、合理地

C 标准库 - `<float.h>`

C 标准库 - <float.h> 概述 <float.h> 是 C 标准库中的一个头文件,它定义了与浮点数类型相关的宏。这些宏提供了关于浮点数的属性信息,如精度、最小和最大值、以及舍入误差等。这个头文件对于需要精确控制浮点数行为的程序非常有用,尤其是在数值计算和科学计算领域。 主要宏 <float.h> 中定义了许多宏,下面列举了一些主要的宏: FLT_RADIX:定义了浮点数的基数。

JVM 常见异常及内存诊断

栈内存溢出 栈内存大小设置:-Xss size 默认除了window以外的所有操作系统默认情况大小为 1MB,window 的默认大小依赖于虚拟机内存。 栈帧过多导致栈内存溢出 下述示例代码,由于递归深度没有限制且没有设置出口,每次方法的调用都会产生一个栈帧导致了创建的栈帧过多,而导致内存溢出(StackOverflowError)。 示例代码: 运行结果: 栈帧过大导致栈内存

模拟实现vector中的常见接口

insert void insert(iterator pos, const T& x){if (_finish == _endofstorage){int n = pos - _start;size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;reserve(newcapacity);pos = _start + n;//防止迭代

【Kubernetes】常见面试题汇总(三)

目录 9.简述 Kubernetes 的缺点或当前的不足之处? 10.简述 Kubernetes 相关基础概念? 9.简述 Kubernetes 的缺点或当前的不足之处? Kubernetes 当前存在的缺点(不足)如下: ① 安装过程和配置相对困难复杂; ② 管理服务相对繁琐; ③ 运行和编译需要很多时间; ④ 它比其他替代品更昂贵; ⑤ 对于简单的应用程序来说,可能不

【附答案】C/C++ 最常见50道面试题

文章目录 面试题 1:深入探讨变量的声明与定义的区别面试题 2:编写比较“零值”的`if`语句面试题 3:深入理解`sizeof`与`strlen`的差异面试题 4:解析C与C++中`static`关键字的不同用途面试题 5:比较C语言的`malloc`与C++的`new`面试题 6:实现一个“标准”的`MIN`宏面试题 7:指针是否可以是`volatile`面试题 8:探讨`a`和`&a`

常见的服务器

常见的Web服务器 1、Tomcat:Tomcat和Java结合得最好,是Oracle官方推荐的JSP服务器。Tomcat是开源的Web服务器,经过长时间的发展,性能、稳定性等方面都非常优秀。 2、Jetty:另一个优秀的Web服务器。Jetty有个更大的优点是,Jetty可作为一个嵌入式服务器,即:如果在应用中加入Jetty的JAR文件,应用可在代码中对外提供Web服务。 3、Resin:

【Kubernetes】常见面试题汇总(一)

目录 1.简述 etcd 及其特点? 2.简述 etcd 适应的场景? 3.简述什么是Kubernetes? 4.简述 Kubernetes和 Docker的关系? 1.简述 etcd 及其特点? (1)etcd 是Core0s 团队发起的开源项目,是一个管理配置信息和服务发现(service discovery)的项目,它的目标是构建一个高可用的分布式键值(keyvalue)数据

《C++标准库》读书笔记/第一天(C++新特性(1))

C++11新特性(1) 以auto完成类型自动推导 auto i=42; //以auto声明的变量,其类型会根据其初值被自动推倒出来,因此一定需要一个初始化操作; static auto a=0.19;//可以用额外限定符修饰 vector<string> v;  auto pos=v.begin();//如果类型很长或类型表达式复杂 auto很有用; auto l=[] (int

CSP-J基础之cmath常见函数

文章目录 前言1. **`sin` 函数**2. **`cos` 函数**3. **`exp` 函数**4. **`log` 函数**5. **`fabs` 函数**6. **`pow` 函数**7. **`sqrt` 函数**8. **`ceil` 函数**9. **`floor` 函数** 总结 前言 在计算机科学与编程中,数学函数是解决各种计算问题的基础工具。C++标准