C# 中的 CRC-4 实现

发布于 2024-08-13 05:04:48 字数 284 浏览 11 评论 0原文

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

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

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

谢谢!

I've been searching the net for a C# implementation of the 4-bit cyclic redundancy check (CRC-4-ITU) but so far I've been unsuccessful.

Is there anyone who's able to give me a reference implementation of CRC-4-ITU? Preferrably with the standard polynomial if there is a standard polynomial (I've read the spec pointed to by wikipedia as the CRC4 spec without finding a definition of the polynomial).

I'd also really appreciate some sort of test suite or test data to verify a CRC4 implementation.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2024-08-20 05:04:48

维基百科的循环冗余检查文章说多项式是 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 算法的所有内容
询问担心你的理解错误可能会被发现。”

The Cyclic Redundancy Check article at Wikipedia says the polynomial is x^4 + x + 1. There is also a pretty good description of how the checksum is computed.

Here is an algorithm for CRC16. I know it's not what you asked for, but it should be relatively straightforward to adapt it for 4 bits.

   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

Also, there is this guide to computing checksums:

http://www.ross.net/crc/download/crc_v3.txt

"Everything you wanted to know about CRC algorithms, but were afraid
to ask for fear that errors in your understanding might be detected."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文