導航:首頁 > 源碼編譯 > crc校驗演算法c語言代碼

crc校驗演算法c語言代碼

發布時間:2022-11-28 23:55:05

Ⅰ crc16 c代碼

0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
0x40
};

static unsigned char auchCRCHi[]=
{
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40
};

bool CRCDecToHex(unsigned char Destination[],unsigned char Source,const int DesLen,int &HexBit);
void UCToASCII(unsigned char Lo16[],int len);
unsigned short CRC16(unsigned char puchmsg[],unsigned short usdatalen);

int main()
{
//unsigned char buf[]={0x01,0xA8,0x98,0x42,0x65,0x74,0x74,0x65,0x72,0x17};

//unsigned char buf[]={0x07,0x11,0x41,0x00,0x53,0xB9,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03};
unsigned char buf[]={0x00,0x00,0x01,0x00,0x00,0x00,0x1A,0x00,0x1C,0x03};
//unsigned short Len=10;
//unsigned short Len=19;
unsigned short Len=10;
unsigned char Lo=CRC16(buf,Len)%256;
unsigned char Hi=CRC16(buf,Len)/256;

unsigned char Lo16[4]={0};
unsigned char Hi16[4]={0};
printf(" 十進制:低位元組Lo=%d\n 十進制:高位元組Hi=%d\n",Lo,Hi);
unsigned char temp=Lo;
int ASCIILoLen=0;
CRCDecToHex(Lo16,temp,4,ASCIILoLen);
UCToASCII(Lo16,ASCIILoLen);

printf(" 低位元組:Lo16=");
for (int i=0;i<ASCIILoLen;i++)
printf("%c",Lo16[i]);
printf("H\n");

temp=Hi;
int ASCIIHiLen=0;
CRCDecToHex(Hi16,temp,4,ASCIIHiLen);
UCToASCII(Hi16,ASCIIHiLen);

printf(" 高位元組:Hi16=");
for (int I=0;I<ASCIIHiLen;I++)
printf("%c",Hi16[I]);
printf("H\n");

return 1;
}

//十進制轉十六進制
bool CRCDecToHex(unsigned char Destination[],unsigned char Source,const int DesLen,int &HexBit)
{
unsigned char temp=Source;
int i=DesLen-1;

while(temp>=16)
{
Destination[i]=temp%16;
printf("Lo[%d]=%d\n",i,Destination[i]);
temp=temp/16;
i--;
}
Destination[i]=temp;
printf("Lo[%d]=%d\n",i,Destination[i]);

HexBit=DesLen-1-i+1;
for(int a=i;(a-i)<(DesLen-1-i+1);a++)
{
Destination[a-i]=Destination[a];
printf("移位後:Lo16[%d]=%d\n",a-i,Destination[a-i]);
}

return true;
}

//無符號字元型轉ASCII碼

void UCToASCII(unsigned char Lo16[],int len)
{
for(int i=0;i<len;i++)
{
if (Lo16[i]>=10)
{
switch (Lo16[i])
{
case 10:
Lo16[i]='A';break;
case 11:
Lo16[i]='B';break;
case 12:
Lo16[i]='C';break;
case 13:
Lo16[i]='D';break;
case 14:
Lo16[i]='E';break;
case 15:
Lo16[i]='F';break;
default: break;
}

}
else
{
Lo16[i]+=48;
}
}
}

unsigned short CRC16(unsigned char puchmsg[],unsigned short usdatalen)
{
unsigned char uchCRCHi=0xFF;
unsigned char uchCRCLo=0xFF;
unsigned short uIndex;
unsigned short y=0;
while(usdatalen--)
{
uIndex=uchCRCHi=(unsigned char)(uchCRCHi ^ puchmsg[y++]);
uchCRCHi=(unsigned char)(uchCRCLo ^ auchCRCHi[uIndex]);
uchCRCLo=auchCRCLo[uIndex];
}
return(unsigned short)(uchCRCLo+uchCRCHi*256);

Ⅱ C語言實現CRC校驗

把我知道的說一下:
碼流後面加8個0可以用移位得到(碼流<<8;)
單次異或運算可以用運算符:^(運算符兩邊為常數)
由於你校驗的是5個位元組,且要多次異或運算,所以得藉助數組,或其它的數據結果才能完成。

最後問一下你是做硬體的嗎

Ⅲ 高分求計算CRC校驗碼的C語言程序

你就是想要CRC8-CCITT的代碼,這個到處都是。
http://www.rajivchakravorty.com/source-code/uncertainty/multimedia-sim/html/crc8_8c-source.html
我一直有CRC16,沒試過這個,但應該差不多。
參考文獻:http://blog.sina.com.cn/s/blog_5e330a280100fcp9.html

Ⅳ 誰有用C語言編寫的CRC-16校驗形式的代碼~·高分求

#define crc16_polynomial 0x8005 // crc_16校驗方式的多項式.

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef enum tagboolean { false, true } bool;

ulong g_ultable[256];

// crc_16方式校驗的初始化函數, 計算crc_16餘數表.
void _far crc16init(void)
{
uint nremainder;
int n, m;
ulong *pultable = g_ultable;

for(n = 0; n < 256; n ++)
{
nremainder = (uint)n << 8;
for(m = 8; m > 0; m --)
{
if(nremainder & 0x8000)
{
nremainder = (nremainder << 1) ^ crc16_polynomial;
}
else
{
nremainder = (nremainder << 1);
}
}
*(pultable + n) = nremainder;
}
}

// 以crc_16方式計算一個數據塊的crc值.
// pucdata - 待校驗的數據塊指針.
// nbytes - 數據塊大小, 單位是位元組.
// 返回值是無符號的長整型, 其中低16位有效.
ulong _far crc16calc(uchar *pucdata, int nbytes)
{
uint nremainder, nret;
int n;
uchar index;
ulong *pultable = g_ultable;

nremainder = 0x0000;
for(n = 0; n < nbytes; n ++)
{
index = (uchar)crcbitreflect(*(pucdata + n), 8) ^ (nremainder >> 8);
nremainder = (uint)*(pultable + index) ^ (nremainder << 8);
}
nret = (uint)crcbitreflect(nremainder, 16) ^ 0x0000;
return(nret);
}

// 反轉數據的比特位, 反轉後msb為1.
// 反轉前: 1110100011101110 0010100111100000
// 反轉後: 1111001010001110 1110001011100000
ulong _far crcbitreflect(ulong uldata, int nbits)
{
ulong ulresult = 0x00000000l;
int n;

for(n = 0; n < nbits; n ++)
{
if(uldata & 0x00000001l)
{
ulresult |= (ulong)(1l << ((nbits - 1) - n));
}
uldata = (uldata >> 1);
}
return(ulresult);
}

Ⅳ 用C語言實現CRC編碼程序

#include <stdio.h>
#include <string.h>
#include "stdlib.h"
unsigned int char2int(char *str)
{
unsigned int count=0, ret=0;
for(count = 0; count<strlen(str);count++)
{
ret = ret<<1;
if('0' != str[count])
{ ret+=1;}
}
return ret;
}

unsigned int getR(char *str)
{
unsigned int c =0 ;
int ret = strlen(str)-1;
for(c=0;c < strlen(str);c++)
{if(str[c] != '0')<br/> {return ret-c;}
}
}

int getRi(unsigned int num)
{
int c =0;
for(;num != 0; c++)
{num = num>>1;}
return c;
}

void CRC(char *scode, char *p, char*g )
{
unsigned int iP = char2int(p);
unsigned int iG = char2int(g);
unsigned int r= getR(g);
unsigned int code = iP << r;
unsigned int yx = code;
for(;getRi(yx) >= getRi(iG);)
{ yx = yx ^ (iG<<(getRi(yx) - getRi(iG)));}
code += yx;
itoa(code,scode,2);
}

void main() //定義主函數
{
char data[8]="" , bds[8]="",code[16]="";
printf("數據:");
scanf("%s", data);
printf("表達式:");
scanf("%s", bds);
CRC(code,data,bds);
printf("編碼:%s",code);
}

Ⅵ CRC的C語言的程序

按位計算CRC採用CRC-CCITT多項式,多項式為0x11021,C語言編程時,參與計算為0x1021。當按位計算CRC時,例如計算二進制序列為1001 1010 1010 1111時,將二進制序列數左移16位,即為1001 1010 1010 1111 (0000 0000 0000 0000),實際上該二進制序列可拆分為1000 0000 0000 0000 (0000 0000 0000 0000) + 000 0000 0000 0000 (0000 0000 0000 0000) + 00 0000 0000 0000 (0000 0000 0000 0000) + 1 0000 0000 0000 (0000 0000 0000 0000) + ……
現在開始分析運算:
<1>對第一個二進制分序列求余數,豎式除法即為0x10000 ^ 0x11021運算,後面的0位保留;
<2>接著對第二個二進制分序列求余數,將第一步運算的余數*2後再和第二個二進制分序列一起對0x11021求余,這一步理解應該沒什麼問題。如果該分序列為0,無需計算。
<3>對其餘的二進制序列求余與上面兩步相同。
<4>計算到最後一位時即為整個二進制序列的余數,即為CRC校驗碼。
該計算方法相當於對每一位計算,運算過程很容易理解,所佔內存少,缺點是一位一位計算比較耗時。
下面給出C語言實現方法:
代碼如下:
unsigned char test[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
unsigned char len = 16;
void main( void )
{
unsigned long temp = 0;
unsigned int crc;
unsigned char i;
unsigned char *ptr = test;

while( len-- ) {
for(i = 0x80; i != 0; i = i >> 1) {
temp = temp * 2;
if((temp & 0x10000) != 0)
temp = temp ^ 0x11021;

if((*ptr & i) != 0)
temp = temp ^ (0x10000 ^ 0x11021);
}
ptr++;
}
crc = temp;
printf("0x%x ",crc);
}

Ⅶ 跪求24位CRC校驗的C語言程序,生成多項式g(x)=x^24+x^23+x^6+x^5+x+1

long int GenerateChecksumCRC24_D32(unsigned long ulNumValues,unsigned long *pulData)
{
unsigned long i,ulData,lfsr = 0xFFFFFF;
for (i= 0x0; i < ulNumValues;i++)
{
ulData = pulData[i];
lfsr = CRC24_D32(lfsr,ulData);
}
return lfsr;
}
static unsigned long CRC24_D32(const unsigned long old_CRC, const unsigned long Data)
{
unsigned long D [32];
unsigned long C [24];
unsigned long NewCRC [24];
unsigned long ulCRC24_D32;
unsigned long int f, tmp;
unsigned long int bit_mask = 0x000001;
tmp = 0x000000;
// Convert previous CRC value to binary.
bit_mask = 0x000001;
for (f = 0; f <= 23; f++)
{
C[f] = (old_CRC & bit_mask) >> f;
bit_mask = bit_mask << 1;
}
// Convert data to binary.
bit_mask = 0x000001;
for (f = 0; f <= 31; f++)
{
D[f] = (Data & bit_mask) >> f;
bit_mask = bit_mask << 1;
}
// Calculate new LFSR value.
NewCRC[0] = D[31] ^ D[30] ^ D[29] ^ D[28] ^ D[27] ^ D[26] ^ D[25] ^
D[24] ^ D[23] ^ D[17] ^ D[16] ^ D[15] ^ D[14] ^ D[13] ^
D[12] ^ D[11] ^ D[10] ^ D[9] ^ D[8] ^ D[7] ^ D[6] ^
D[5] ^ D[4] ^ D[3] ^ D[2] ^ D[1] ^ D[0] ^ C[0] ^ C[1] ^
C[2] ^ C[3] ^ C[4] ^ C[5] ^ C[6] ^ C[7] ^ C[8] ^ C[9] ^
C[15] ^ C[16] ^ C[17] ^ C[18] ^ C[19] ^ C[20] ^ C[21] ^
C[22] ^ C[23];
NewCRC[1] = D[23] ^ D[18] ^ D[0] ^ C[10] ^ C[15];
NewCRC[2] = D[24] ^ D[19] ^ D[1] ^ C[11] ^ C[16];
NewCRC[3] = D[25] ^ D[20] ^ D[2] ^ C[12] ^ C[17];
NewCRC[4] = D[26] ^ D[21] ^ D[3] ^ C[13] ^ C[18];
NewCRC[5] = D[31] ^ D[30] ^ D[29] ^ D[28] ^ D[26] ^ D[25] ^ D[24] ^
D[23] ^ D[22] ^ D[17] ^ D[16] ^ D[15] ^ D[14] ^ D[13] ^
D[12] ^ D[11] ^ D[10] ^ D[9] ^ D[8] ^ D[7] ^ D[6] ^
D[5] ^ D[3] ^ D[2] ^ D[1] ^ D[0] ^ C[0] ^ C[1] ^ C[2] ^
C[3] ^ C[4] ^ C[5] ^ C[6] ^ C[7] ^ C[8] ^ C[9] ^ C[14] ^
C[15] ^ C[16] ^ C[17] ^ C[18] ^ C[20] ^ C[21] ^ C[22] ^
C[23];
LFSR代碼示例
簽名是一個多項式為x24+ x23+ x6
+ x5
+x+1的24位CRC。初始值為0xFFFFFF。
AN-1160
Rev. A | Page 7 of 8
NewCRC[6] = D[28] ^ D[18] ^ D[5] ^ D[0] ^ C[10] ^ C[20];
NewCRC[7] = D[29] ^ D[19] ^ D[6] ^ D[1] ^ C[11] ^ C[21];
NewCRC[8] = D[30] ^ D[20] ^ D[7] ^ D[2] ^ C[12] ^ C[22];
NewCRC[9] = D[31] ^ D[21] ^ D[8] ^ D[3] ^ C[0] ^ C[13] ^ C[23];
NewCRC[10] = D[22] ^ D[9] ^ D[4] ^ C[1] ^ C[14];
NewCRC[11] = D[23] ^ D[10] ^ D[5] ^ C[2] ^ C[15];
NewCRC[12] = D[24] ^ D[11] ^ D[6] ^ C[3] ^ C[16];
NewCRC[13] = D[25] ^ D[12] ^ D[7] ^ C[4] ^ C[17];
NewCRC[14] = D[26] ^ D[13] ^ D[8] ^ C[0] ^ C[5] ^ C[18];
NewCRC[15] = D[27] ^ D[14] ^ D[9] ^ C[1] ^ C[6] ^ C[19];
NewCRC[16] = D[28] ^ D[15] ^ D[10] ^ C[2] ^ C[7] ^ C[20];
NewCRC[17] = D[29] ^ D[16] ^ D[11] ^ C[3] ^ C[8] ^ C[21];
NewCRC[18] = D[30] ^ D[17] ^ D[12] ^ C[4] ^ C[9] ^ C[22];
NewCRC[19] = D[31] ^ D[18] ^ D[13] ^ C[5] ^ C[10] ^ C[23];
NewCRC[20] = D[19] ^ D[14] ^ C[6] ^ C[11];
NewCRC[21] = D[20] ^ D[15] ^ C[7] ^ C[12];
NewCRC[22] = D[21] ^ D[16] ^ C[8] ^ C[13];
NewCRC[23] = D[31] ^ D[30] ^ D[29] ^ D[28] ^ D[27] ^ D[26] ^ D[25] ^
D[24] ^ D[23] ^ D[22] ^ D[16] ^ D[15] ^ D[14] ^ D[13] ^
D[12] ^ D[11] ^ D[10] ^ D[9] ^ D[8] ^ D[7] ^ D[6] ^
D[5] ^ D[4] ^ D[3] ^ D[2] ^ D[1] ^ D[0] ^ C[0] ^ C[1] ^
C[2] ^ C[3] ^ C[4] ^ C[5] ^ C[6] ^ C[7] ^ C[8] ^ C[14] ^
C[15] ^ C[16] ^ C[17] ^ C[18] ^ C[19] ^ C[20] ^ C[21] ^
C[22] ^ C[23];
ulCRC24_D32 = 0;
// LFSR value from binary to hex.
bit_mask = 0x000001;
for (f = 0; f <= 23; f++)
{
ulCRC24_D32 = ulCRC24_D32 + NewCRC[f] * bit_mask;
bit_mask = bit_mask << 1;
}
return(ulCRC24_D32 & 0x00FFFFFF);
}

Ⅷ crc16校驗的c語言程序

下面我們以CRC-16為例來說明任意長度數據流的CRC校驗碼生成過程。我們採用將數據流分成若干個8bit字元,並由低位元組到高位元組傳送的並行方法來求CRC校驗碼。具體計算過程為:用一個16bit的寄存器來存放CRC校驗值,且設定其初值為0x0000;將數據流的第一個8bit與16bit的CRC寄存器的高位元組相異或,並將結果存入CRC寄存器高位元組;CRC寄存器左移一位,最低1bit補零,同時檢查移出的最高1bit,若移出的最高1bit為0,則繼續按上述過程左移,若最高1bit為1,則將CRC寄存器中的值與生成多項式碼相異或,結果存入CRC寄存器值;繼續左移並重復上述處理方法,直到將8bit數據處理完為止,則此時CRC寄存器中的值就是第一個8bit數據對應的CRC校驗碼;然後將此時CRC寄存器的值作為初值,用同樣的處理方法重復上述步驟來處理下一個8bit數據流,直到將所有的8bit字元都處理完後,此刻CRC寄存器中的值即為整個數據流對應的CRC校驗碼。
下面示出了其計算過程的流程圖:

在用C語言編寫CRC校驗碼的實現程序時我們應該注意,生成多項式 對應的十六進制數為0x18005,由於CRC寄存器左移過程中,移出的最高位為1時與 相異或,所以與16bit的CRC寄存器對應的生成多項式的十六進制數可用0x8005表示。下面給出並行處理8bit數據流的C源程序:
unsigned short crc_dsp(unsigned short reg, unsigned char data_crc)
//reg為crc寄存器, data_crc為將要處理的8bit數據流
{
unsigned short msb; //crc寄存器將移出的最高1bit
unsigned short data;
unsigned short gx = 0x8005, i = 0; //i為左移次數, gx為生成多項式

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);
}
以上為處理每一個8bit數據流的子程序,在計算整個數據流的CRC校驗碼時,我們只需將CRC_reg的初值置為0x0000,求第一個8bit的CRC值,之後,即可將上次求得的CRC值和本次將要處理的8bit數據作為函數實參傳遞給上述子程序的形參進行處理即可,最終返回的reg值便是我們所想得到的整個數據流的CRC校驗值。

Ⅸ c語言 CRC的檢驗方式 我想問一下。這下面的C語言返回的CRC的值是什麼。他有他的公式是怎麼樣的

CRC又稱循環冗餘校驗,CRC返回的值其實是校驗位,校驗位分高位和低位。
實際應用時,發送裝置計算出CRC值並隨數據一同發送給接收裝置,接收裝置對收到的數據重新計算CRC並與收到的CRC相比較,若兩個CRC值不同,則說明數據通訊出現錯誤。

閱讀全文

與crc校驗演算法c語言代碼相關的資料

熱點內容
him觸摸編程軟體 瀏覽:929
植物大戰僵屍存檔怎麼轉移安卓 瀏覽:852
java棧的元素 瀏覽:737
程序員與籃球事件 瀏覽:675
app反編譯不完整 瀏覽:788
電腦上的文件夾怎麼調整 瀏覽:7
伺服器無響應是什麼原因呀 瀏覽:984
wd文檔里的app怎麼製作 瀏覽:513
電腦里的文件夾沒有了一般能恢復嗎 瀏覽:418
哪裡有配加密鑰匙的 瀏覽:210
伺服器開不了機怎麼把數據弄出來 瀏覽:958
gif動態圖片怎麼壓縮 瀏覽:521
黑猴子棒球壓縮文件解壓密碼 瀏覽:631
如何讓app適應不同的手機屏幕大小 瀏覽:10
蘋果手機如何給安卓手機分享軟體 瀏覽:761
蘋果電腦怎麼運行騰訊雲伺服器 瀏覽:59
明日之後沙石堡命令助手 瀏覽:261
蛋糕店用什麼樣的app 瀏覽:877
長安銀行信用卡app怎麼取現 瀏覽:635
dos命令cmd命令的 瀏覽:226