我一直在网上搜索4位循环冗余校验(CRC-4-ITU)的C#实现,但到目前为止我还没有成功。
是否有人能够为我提供CRC-4-ITU的参考实施?如果存在标准多项式,那么最好使用标准多项式(我已经将规范pointed to by wikipedia读作CRC4规范而没有找到多项式的定义)。
我也非常感谢某种测试套件或测试数据来验证CRC4的实现。
谢谢!
答案 0 :(得分:3)
维基百科上的Cyclic Redundancy Check文章说多项式是x ^ 4 + x + 1.还有很好的描述如何计算校验和。
这是CRC16的算法。我知道这不是你要求的,但它应该相对简单地适应4位。
public ushort calculate(byte[] bytes)
{
int crc = 0xFFFF; // initial value
// loop, calculating CRC for each byte of the string
for (int byteIndex = 0; byteIndex < bytes.Length; byteIndex++)
{
ushort bit = 0x80; // initialize bit currently being tested
for (int bitIndex = 0; bitIndex < 8; bitIndex++)
{
bool xorFlag = ((crc & 0x8000) == 0x8000);
crc <<= 1;
if (((bytes[byteIndex] & bit) ^ (ushort)0xff) != (ushort)0xff)
{
crc = crc + 1;
}
if (xorFlag)
{
crc = crc ^ 0x1021;
}
bit >>= 1;
}
}
return (ushort)crc;
}
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24775723.html
此外,还有计算校验和的指南:
http://www.ross.net/crc/download/crc_v3.txt
“你想知道关于CRC算法的一切,但是害怕 要求担心可能会发现你理解中的错误。“