4

我一直在网上搜索 4 位循环冗余校验 (CRC-4-ITU) 的 C# 实现,但到目前为止我还没有成功。

有没有人能给我CRC-4-ITU的参考实现?如果有标准多项式,最好使用标准多项式(我已经阅读了维基百科指出的规范作为 CRC4 规范,但没有找到多项式的定义)。

我也非常感谢某种测试套件或测试数据来验证 CRC4 实现。

谢谢!

4

1 回答 1

3

Wikipedia 上的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 算法的一切,但害怕问你理解的错误可能会被检测到。”

于 2009-12-02T17:48:07.127 回答