crc16计算

unsigned short sp_crc16_with_init(unsigned short crc, const unsigned char *buf, int size)

{

  unsigned char i;

  while(size--!=0)

  {

    for(i=0x80; i!=0; i/=2)

    {

      if((crc&0x8000)!=0) 

      {

          crc*=2; 

          crc^=0x1021;

      } /* 余式CRC乘以2再求CRC */

      else 

      {

          crc*=2;

      }

      if((*buf&i)!=0) 

      {

          crc^=0x1021; /* 再加上本位的CRC */

      }

    }

    buf++;

  }

  return(crc);

}

unsigned short sp_crc16(const unsigned char *buf, int  size)

{

  return(sp_crc16_with_init(0x00,buf,size));

}

你可能感兴趣的:(C语言)