下面我們以CRC-16為例來說明任意長度數(shù)據(jù)流的CRC校驗(yàn)碼生成過程。我們采用將數(shù)據(jù)流分成若干個(gè)8bit字符,并由低字節(jié)到高字節(jié)傳送的并行方法來求CRC校驗(yàn)碼。具體計(jì)算過程為:用一個(gè)16bit的寄存器來存放CRC校驗(yàn)值,且設(shè)定其初值為0x0000;將數(shù)據(jù)流的第一個(gè)8bit與16bit的CRC寄存器的高字節(jié)相異或,并將結(jié)果存入CRC寄存器高字節(jié);CRC寄存器左移一位,最低1bit補(bǔ)零,同時(shí)檢查移出的最高1bit,若移出的最高1bit為0,則繼續(xù)按上述過程左移,若最高1bit為1,則將CRC寄存器中的值與生成多項(xiàng)式碼相異或,結(jié)果存入CRC寄存器值;繼續(xù)左移并重復(fù)上述處理方法,直到將8bit數(shù)據(jù)處理完為止,則此時(shí)CRC寄存器中的值就是第一個(gè)8bit數(shù)據(jù)對應(yīng)的CRC校驗(yàn)碼;然后將此時(shí)CRC寄存器的值作為初值,用同樣的處理方法重復(fù)上述步驟來處理下一個(gè)8bit數(shù)據(jù)流,直到將所有的8bit字符都處理完后,此刻CRC寄存器中的值即為整個(gè)數(shù)據(jù)流對應(yīng)的CRC校驗(yàn)碼。
10年的融安網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整融安建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“融安網(wǎng)站設(shè)計(jì)”,“融安網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
下面示出了其計(jì)算過程的流程圖:
在用C語言編寫CRC校驗(yàn)碼的實(shí)現(xiàn)程序時(shí)我們應(yīng)該注意,生成多項(xiàng)式 對應(yīng)的十六進(jìn)制數(shù)為0x18005,由于CRC寄存器左移過程中,移出的最高位為1時(shí)與 相異或,所以與16bit的CRC寄存器對應(yīng)的生成多項(xiàng)式的十六進(jìn)制數(shù)可用0x8005表示。下面給出并行處理8bit數(shù)據(jù)流的C源程序:
unsigned short crc_dsp(unsigned short reg, unsigned char data_crc)
//reg為crc寄存器, data_crc為將要處理的8bit數(shù)據(jù)流
{
unsigned short msb; //crc寄存器將移出的最高1bit
unsigned short data;
unsigned short gx = 0x8005, i = 0; //i為左移次數(shù), gx為生成多項(xiàng)式
data = (unsigned short)data_crc;
data = data 8;
reg = reg ^ data;
do
{
msb = reg 0x8000;
reg = reg 1;
if(msb == 0x8000)
{
reg = reg ^ gx;
}
i++;
}
while(i 8);
return (reg);
}
以上為處理每一個(gè)8bit數(shù)據(jù)流的子程序,在計(jì)算整個(gè)數(shù)據(jù)流的CRC校驗(yàn)碼時(shí),我們只需將CRC_reg的初值置為0x0000,求第一個(gè)8bit的CRC值,之后,即可將上次求得的CRC值和本次將要處理的8bit數(shù)據(jù)作為函數(shù)實(shí)參傳遞給上述子程序的形參進(jìn)行處理即可,最終返回的reg值便是我們所想得到的整個(gè)數(shù)據(jù)流的CRC校驗(yàn)值。
unsigned short crc_dsp(unsigned short reg, unsigned char data_crc)
//reg為crc寄存器, data_crc為將要處理的8bit數(shù)據(jù)流
{
unsigned short msb; //crc寄存器將移出的最高1bit
unsigned short data;
unsigned short gx = 0x8005, i = 0; //i為左移次數(shù), gx為生成多項(xiàng)式
data = (unsigned short)data_crc;
data = data 8;
reg = reg ^ data;
do
{
msb = reg 0x8000;
reg = reg 1;
if(msb == 0x8000)
{
reg = reg ^ gx;
}
i++;
}
while(i 8);
return (reg);
}
int cal_crc(unsigned char *ptr, unsigned char len)
{
unsigned char i;
unsigned int crc_value =0;
while(len--)
{
for(i=0x80; i!=0; i=1 )
{
if (crc_value0x8000)
crc_value = (crc_value 1) ^0x8005 ;
else
crc_value = crc_value 1 ;
if(*ptri)
另外,站長團(tuán)上有產(chǎn)品團(tuán)購,便宜有保證
//函數(shù)功能:計(jì)算CRC16
//參數(shù)1:*pData 16位CRC校驗(yàn)數(shù)據(jù),
//參數(shù)2:nLength 數(shù)據(jù)流長度
//參數(shù)3:init 初始化值
//參數(shù)4:ptable 16位CRC查找表
//逆序CRC計(jì)算
unsigned short GetRevCrc_16(unsigned char * pData, int nLength,
unsigned short init, const
unsigned short *ptable)
{
unsigned short cRc_16 = init;
unsigned char temp;
while(nLength-- 0)
{
temp = cRc_16 0xFF;
cRc_16 = (cRc_16 8) ^ ptable[(temp ^ *pData++) 0xFF];
}
return cRc_16;
}
//正序CRC計(jì)算
unsigned short GetCrc_16(unsigned char * pData, int nLength,
unsigned short init, const
unsigned short *ptable)
{
unsigned short cRc_16 = init;
unsigned char temp;
while(nLength-- 0)
{
temp = cRc_16 8;
cRc_16 = (cRc_16 8) ^ ptable[(temp ^ *pData++) 0xFF];
}
return cRc_16;
}
//Demo -- modbus-crc16測試
unsigned short CRC_GetModbus16(unsigned char *pdata, int len)
{
//MODBUS CRC-16表 8005 逆序
const unsigned short g_McRctable_16[256] =
{
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
};
return GetRevCrc_16(pdata, len, 0xFFFF, g_McRctable_16);
}