问题描述
我正在尝试为12位CRC和算法创建crc_table,但始终会得到错误的结果。
I am trying to do crc_table for 12-bit CRC and the algorithm, but always I got wrong results.
您能帮我吗?要创建crc表,我尝试:
Can you help me? To create crc table I try:
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算法是:
I updated that and CRC algorithm is:
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);
}
但是它不起作用.....
But It isn't working.....
推荐答案
这似乎不正确:
if (remainder & 10000000)
您似乎打算将此数字用作是二进制,但实际上是十进制。您应该使用十六进制字面量(0x80)。
It looks like you are intending this number to be binary, but it is actually decimal. You should use a hex literal instead (0x80).
此数字以及您执行的移位大小似乎也存在问题:此测试应检查如果余数的高位被设置。由于您要执行12位CRC,因此掩码应为0x800(二进制100000000000)。高于此的偏移应该是:
There also seem to be problems with this number, and with the size of the shift you do: This test should check if the high-order bit of the remainder is set. Since you are doing a 12-bit CRC, the mask should be 0x800 (binary 100000000000). And the shift above that should probably be:
remainder = dividend << 4;
设置其余部分的最左8位。
to set the leftmost 8 bits of the remainder.
这篇关于算法CRC-12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!