我正在尝试对 12 位 CRC 和算法执行 crc_table,但总是得到错误的结果。
你能帮助我吗?要创建 crc 表,我尝试:
void crcInit(void)
{
unsigned short remainder;
int dividend;
unsigned char bit;
for (dividend = 0; dividend < 256; ++dividend)
{
remainder = dividend << 4;
for (bit = 8; bit > 0; --bit)
{
if (remainder & 0x800)
{
remainder = (remainder << 1) ^ 0x180D; //Polynomio of CRC-12
}
else
{
remainder = (remainder << 1);
}
}
crcTable[dividend] = remainder;
}
}
我更新了,CRC 算法是:
unsigned short crcFast(unsigned char const message[], int nBytes)
{
unsigned short remainder = 0x0000;
unsigned char data;
int byte;
/*
* Divide the message by the polynomial, a byte at a time.
*/
for (byte = 0; byte < nBytes; ++byte)
{
data = message[byte] ^ (remainder >> 4);
remainder = crcTable[data] ^ (remainder << 8);
}
/*
* The final remainder is the CRC.
*/
return (remainder ^ 0);
}
但它不起作用......
这似乎不对:
if (remainder & 10000000)
看起来您希望这个数字是二进制的,但实际上它是十进制的。您应该使用十六进制文字 (0x80)。
这个数字以及您所做的移位的大小似乎也存在问题:此测试应检查余数的高位是否已设置。由于您正在执行 12 位 CRC,因此掩码应为 0x800(二进制 100000000000)。上面的转变可能应该是:
remainder = dividend << 4;
设置余数的最左边 8 位。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)