#define crc_mul 0x1021 /*生成多项式*/
unsigned int cal_crc(unsigned char *ptr,unsigned char len) {
unsigned char i;
unsigned int crc=0;
while(len--!=0) {
for(i=0x80; i!=0; i/=2) {
if((crc&0x8000)!=0) {crc*=2; crc^=crc_mul;} /* 余式CRC乘以2再求CRC */
else crc*=2;
if((*ptr&i)!=0) crc^=crc_mul; /* 再加上本位的CRC */
}
ptr++;
}
return(crc);
}
/*测试代码*/
main()
{
unsigned char i[8] = { 0x00,0x00,0x00,0x00,0x06,0x0d,0xd2,0xe3};/*{0x12,0x23,0xa5,0x86,0x07};*/
unsigned int crc;
crc=cal_crc(&i,8);
printf("%x",crc);
getch();
}
阅读(11669) | 评论(0) | 转发(0) |