CRC16的C语言实现

 #include<stdio.h>
unsigned int crc16(unsigned char ch,unsigned int crc);
struct Data_32
{
 unsigned __int32 bit0 : 8;
 unsigned __int32 bit1 : 8;
 unsigned __int32 bit2 : 8;
 unsigned __int32 bit3 : 8;
};
union CRC_Data
{
 struct Data_32 data;
 unsigned __int32 data_int;
};
int main()
{
 unsigned __int8 i=0,j=0;
 unsigned __int16 tmp;
  union CRC_Data crc_reg,crc_data;
 crc_reg.data_int=0xffff0000;
 crc_data.data_int=0xcdab;
  
 for(j=0;j<2;j++)
 {
  crc_reg.data.bit2^=crc_data.data.bit0;
  i=8;
  while(i>0)
  {
   crc_reg.data_int>>=1;
   if((crc_reg.data_int&0x8000)!=0)
   {
    crc_reg.data_int^=0xA0010000;
   }
   i--;
  }
  crc_data.data.bit0=crc_data.data.bit1;
 }
 
 printf("%x\t%x\n",crc_reg.data.bit2,crc_reg.data.bit3);
 tmp=crc16(0x00ab,0xffff);
 tmp=crc16(0x00cd,tmp);
 tmp=crc16(0x00bf,tmp);
 tmp=crc16(0x0015,tmp);
 printf("%x\n",tmp);
 return 0;
}
unsigned int crc16(unsigned char ch,unsigned int crc)    
{    
 crc ^=ch;    
 ch =8;    
 while(ch--)    
    {    
     if(crc&0x01)    
  {    
   crc >>=1;    
   crc ^=0xa001;    
  }    
  else
  {    
   crc >>=1;    
  }    
    }    
 return crc;    
}   


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值