算法CRC-12

我正在尝试为12位CRC和算法做crc_table,但总是得到错误的结果。

你能帮助我吗? 要创建crc表我尝试:

void crcInit(void) { unsigned short remainder; int dividend; unsigned char bit; for (dividend = 0; dividend < 256; ++dividend) { remainder = dividend < 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 > 4); remainder = crcTable[data] ^ (remainder << 8); } /* * The final remainder is the CRC. */ return (remainder ^ 0); } 

但它没有工作…..

这似乎不对:

 if (remainder & 10000000) 

看起来你打算这个数字是二进制的,但它实际上是十进制的。 您应该使用hex文字(0x80)。

这个数字似乎也有问题,并且你要做的转移大小:这个测试应检查是否设置了余数的高位。 由于您正在进行12位CRC,因此掩码应为0x800(二进制100000000000)。 而这种转变可能应该是:

 remainder = dividend << 4; 

设置剩余部分的最左边8位。

Boost库将具有已经实现的CRC校验和算法,该算法可以与不同的多项式一起用于除法和比特数。 使用此链接获取更多信息Boost CRC 。

我自己的一个示例实现是:

 string data = "S95I"; boost::crc_optimal<11, 0x571> crc; crc.process_bytes(data.data(), data.size()); stringstream checksum; checksum << (int)crc() % 1296; string resultCheck = checksum.str(); 

要使用12位CRC,必须采用位数和使用的多项式,可在此处找到: Wikipedia CRC多项式