C语言CRC-16 XMODEM格式校验函数( 二 )


uint16_t PY_CRC_16_T16_XMODEM(uint16_t *di, uint32_t len){uint16_t crc_poly = 0x1021;//X^16+X^12+X^5+1 total 16 effective bits without X^16. uint16_t data_t = 0; //CRC registeruint16_t cdata[len];for(uint32_t j=0;j>8 | di[j]<<8);}for(uint32_t i = 0; i < len; i++){data_t ^= cdata[i]; //16-bit datafor (uint8_t j = 0; j < 16; j++){if (data_t & 0x8000)data_t = (data_t << 1) ^ crc_poly;elsedata_t <<= 1;}}return (data_t);}
CRC-16 格式校验函数四(8位输入数据格式):

C语言CRC-16 XMODEM格式校验函数

文章插图
uint16_t PY_CRC_16_T8_XMODEM(uint8_t *di, uint32_t len){uint16_t crc_poly = 0x1021;//X^16+X^12+X^5+1 total 16 effective bits without X^16. uint16_t data_t = 0; //CRC registerfor(uint32_t i = 0; i < len; i++){data_t ^= di[i]<<8; //8-bit datafor (uint8_t j = 0; j < 8; j++){if (data_t & 0x8000)data_t = (data_t << 1) ^ crc_poly;elsedata_t <<= 1;}}return (data_t);}
算法验证
4种算法结果相同:
通过在线CRC工具对照验证成功:
–End–