Ymodem CRC16-CCITT实现

2019-12-14 12:15发布

unsigned short UpdateCRC16(unsigned short crcIn, unsigned char byte)
{
  unsigned int crc = crcIn;      
  unsigned int in = byte|0x100;   

  do
  {  
    crc <<= 1;
    in <<= 1;      

    if(in&0x100)   
    {
      ++crc;      
    }

    if(crc&0x10000)
    {
      crc ^= 0x1021;
    }
} while(!(in&0x10000));

return (crc&0xffffu);
}


unsigned short Cal_CRC16(const unsigned char* data, unsigned int size)
{
  uint32_t crc = 0;
  const unsigned char* dataEnd = data+size;

  while(data<dataEnd)
  {
    crc = UpdateCRC16(crc,*data++);  
  }
  crc = UpdateCRC16(crc,0);
  crc = UpdateCRC16(crc,0);

  return (crc&0xffffu);
}
0条回答

一周热门 更多>