C#中的CRC-4实现 [英] CRC-4 implementation in C#

查看:166
本文介绍了C#中的CRC-4实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有没有人可以给我CRC-4-ITU的参考实现?如果有标准多项式,最好使用标准多项式(我已阅读规范 = nofollow noreferrer>指出它为CRC4规范,但未找到多项式的定义。

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).

我也非常感谢您测试套件或测试数据以验证CRC4实现。

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

谢谢!

推荐答案

Wikipedia上的循环冗余校验文章称多项式为x ^ 4 + x + 1.还很好地描述了校验和的计算方式。

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.

这里是CRC16的算法。我知道这不是您要的,但是将其适应4位应该相对简单。

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

您想知道的有关CRC算法的所有信息,但害怕
担心会检测到您理解中的错误。

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

这篇关于C#中的CRC-4实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆