CRC码与实现兼容性
CRC校验和的机制或步骤很简单,但软件有点多,软件中有些步骤与CRC的步骤不兼容 下图是获取CRC校验和的步骤(它只是一个模2除法): 校验和是余数=001CRC码与实现兼容性,c,embedded,C,Embedded,CRC校验和的机制或步骤很简单,但软件有点多,软件中有些步骤与CRC的步骤不兼容 下图是获取CRC校验和的步骤(它只是一个模2除法): 校验和是余数=001 用于计算CRC校验和的软件适用于一串位: /* * The width of the CRC calculation and result. * Modify the typedef for a 16 or 32-bit CRC standard. */ typedef uint8_t crc; #define WIDTH (8
用于计算CRC校验和的软件适用于一串位:
/*
* The width of the CRC calculation and result.
* Modify the typedef for a 16 or 32-bit CRC standard.
*/
typedef uint8_t crc;
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
crc
crcSlow(uint8_t const message[], int nBytes)
{
crc remainder = 0;
/*
* Perform modulo-2 division, a byte at a time.
*/
for (int byte = 0; byte < nBytes; ++byte)
{
/*
* Bring the next byte into the remainder.
*/
remainder ^= (message[byte] << (WIDTH - 8));
/*
* Perform modulo-2 division, a bit at a time.
*/
for (uint8_t bit = 8; bit > 0; --bit)
{
/*
* Try to divide the current data bit.
*/
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
}
/*
* The final remainder is the CRC result.
*/
return (remainder);
}
/*
*CRC计算的宽度和结果。
*修改16位或32位CRC标准的typedef。
*/
类型定义uint8_t crc;
#定义宽度(8*sizeof(crc))
#定义顶层(1代码示例似乎使用可变大小的CRC,其中CRC的大小为宽度。多项式是宽度+1位多项式的底部宽度位,其最低有效位设置为1。由于操作为XOR,因此数据位与余数的异或顺序无关紧要,因此8个数据位可以是XOR'ed同时转换为余数的高位。然后一次反馈循环中的位出现8位。由于多项式的底位是1,只要数据中有1位,这将保持循环进行。没有CRC。有非常不同的CRC算法。阅读并遵循建议。什么是“模2除”?crc不是。有一些很好的文章描述了crc的思想及其实现。你仍然应该阅读!哦,你的代码可以为int
和crc
的某些大小组合调用未定义的行为。