"); //-->
CRC16常见的标准有以下几种,被用在各个规范中,其算法原理基本一致,就是在数据的输入和输出有所差异,下边把这些标准的差异列出,并给出C语言的算法实现。
CRC16_CCITT:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0x0000异或
CRC16_CCITT_FALSE:多项式x16+x12+x5+1(0x1021),初始值0xFFFF,低位在后,高位在前,结果与0x0000异或
CRC16_XMODEM:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在后,高位在前,结果与0x0000异或
CRC16_X25:多项式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
CRC16_MODBUS:多项式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0x0000异或
CRC16_IBM:多项式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0x0000异或
CRC16_MAXIM:多项式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在后,结果与0xFFFF异或
CRC16_USB:多项式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在后,结果与0xFFFF异或
模式 | 多项式 | 初始值 | 数据位序 | 结果处理 |
CRC16_CCITT | x16+x12+x5+1(0x1021) | 0x0000 | 低位在前,高位在后 | 与0x0000异或 |
CRC16_CCITT_FALSE | x16+x12+x5+1(0x1021) | 0xFFFF | 低位在后,高位在前 | 与0x0000异或 |
CRC16_XMODEM | x16+x12+x5+1(0x1021) | 0x0000 | 低位在后,高位在前 | 与0x0000异或 |
CRC16_X25 | x16+x12+x5+1(0x1021) | 0x0000 | 低位在后,高位在前 | 与0xFFFF异或 |
CRC16_ MODBUS | x16+x15+x2+1(0x8005) | 0xFFFF | 低位在前,高位在后 | 与0x0000异或 |
CRC16_ IBM | x16+x15+x2+1(0x8005) | 0x0000 | 低位在前,高位在后 | 与0x0000异或 |
CRC16_ MAXIM | x16+x15+x2+1(0x8005) | 0x0000 | 低位在前,高位在后 | 与0xFFFF异或 |
CRC16_ USB | x16+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校验码。
根据算法原理与标准要求就能简单的写出具体程序:
unsigned short CRC16_CCITT(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0x0000; unsigned short wCPoly = 0x1021; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8(&wChar,&wChar); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint16(&wCRCin,&wCRCin); return (wCRCin) ; } unsigned short CRC16_CCITT_FALSE(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0xFFFF; unsigned short wCPoly = 0x1021; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } return (wCRCin) ; } unsigned short CRC16_XMODEM(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0x0000; unsigned short wCPoly = 0x1021; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } return (wCRCin) ; } unsigned short CRC16_X25(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0xFFFF; unsigned short wCPoly = 0x1021; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8(&wChar,&wChar); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint16(&wCRCin,&wCRCin); return (wCRCin^0xFFFF) ; } unsigned short CRC16_MODBUS(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0xFFFF; unsigned short wCPoly = 0x8005; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8(&wChar,&wChar); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint16(&wCRCin,&wCRCin); return (wCRCin) ; } unsigned short CRC16_IBM(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0x0000; unsigned short wCPoly = 0x8005; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8(&wChar,&wChar); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint16(&wCRCin,&wCRCin); return (wCRCin) ; } unsigned short CRC16_MAXIM(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0x0000; unsigned short wCPoly = 0x8005; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8(&wChar,&wChar); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint16(&wCRCin,&wCRCin); return (wCRCin^0xFFFF) ; } unsigned short CRC16_USB(unsigned char *puchMsg, unsigned int usDataLen) { unsigned short wCRCin = 0xFFFF; unsigned short wCPoly = 0x8005; unsigned char wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8(&wChar,&wChar); wCRCin ^= (wChar << 8); for(int i = 0;i < 8;i++) { if(wCRCin & 0x8000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint16(&wCRCin,&wCRCin); return (wCRCin^0xFFFF) ; }
void InvertUint8(unsigned char *dBuf,unsigned char *srcBuf) { int i; unsigned char tmp[4]; tmp[0] = 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]; tmp[0] = 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]; tmp[0] = 0; for(i=0;i< 32;i++) { if(srcBuf[0]& (1 << i)) tmp[0]|=1<<(15 - i); } dBuf[0] = tmp[0]; }
具体验证使用这个工具,内含CRC算法的计算,和后边的博客中提到的其他算法的工具合集
在这个基础上也加入CRC32 的校验算法
/CRC32算法: unsigned int CRC32(unsigned char *puchMsg, unsigned int usDataLen) { int i; unsigned int wCRCin = 0xFFFFFFFF; unsigned int wCPoly = 0x04C11DB7; unsigned int wChar = 0; while (usDataLen--) { wChar = *(puchMsg++); InvertUint8((unsigned char *)&wChar,(unsigned char *)&wChar); wCRCin ^= (wChar << 24); for(i = 0;i < 8;i++) { if(wCRCin & 0x80000000) wCRCin = (wCRCin << 1) ^ wCPoly; else wCRCin = wCRCin << 1; } } InvertUint32(&wCRCin,&wCRCin); return (wCRCin ^ 0xFFFFFFFF) ; }
对于CRC32可能还有其他的多项式和初始值和结果值是否需要异或以及输入数据是否需要位序倒转等要求在源码中修改
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。