前段时间一直在使用CRC校验函数,现在将函数内容总结:
typedef unsigned short uint16_t
typedef unsigned char uint8_t
typedef unsigned int uint32_t
//CRC校验函数
//参数*pData:需要进行校验的数组
//参数plen: 需要进行校验的数组数据长度
//返回值siRet: CRC校验结果
uint16_t checkCRC(uint8_t *pData, uint32_t plen)
{
if (NULL == pData || plen <= 0)
{
return 0;
}
uint16_t u16CRC = 0xFFFF;
for (int i = 0; i < plen; i++)
{
u16CRC ^= (uint16_t)(pData[i]);
for(int j = 0; j <= 7; j++)
{
if (u16CRC & 0x0001)
{
u16CRC = (u16CRC >> 1) ^ 0xA001;
}
else
{
u16CRC = u16CRC >> 1;
}
}
}
uint16_t siRet = 0;
siRet = (u16CRC & 0x00FF) << 8;
siRet |= u16CRC >> 8;
return siRet;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务