首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误的CRC8校验和计算

错误的CRC8校验和计算
EN

Stack Overflow用户
提问于 2017-06-27 03:49:34
回答 2查看 501关注 0票数 1

我让这个类来计算byte[]的CRC8校验和:

代码语言:javascript
复制
public static class Crc8
    {
        static byte[] table = new byte[256];
        // x8 + x7 + x6 + x4 + x2 + 1
        const byte poly = 0xd5;

        public static byte ComputeChecksum(params byte[] bytes)
        {
            byte crc = 0;
            if (bytes != null && bytes.Length > 0)
            {
                foreach (byte b in bytes)
                {
                    crc = table[crc ^ b];
                }
            }
            return crc;
        }

        static Crc8()
        {
            for (int i = 0; i < 256; ++i)
            {
                int temp = i;
                for (int j = 0; j < 8; ++j)
                {
                    if ((temp & 0x80) != 0)
                    {
                        temp = (temp << 1) ^ poly;
                    }
                    else
                    {
                        temp <<= 1;
                    }
                }
                table[i] = (byte)temp;
            }
        }
    }

大体上我得到了:

代码语言:javascript
复制
static void Main(string[] args)
{

    string number = "123456789";



    Console.WriteLine(Convert.ToByte(Crc8.ComputeChecksum(StringToByteArray(number))).ToString("x2"));

    Console.ReadLine();

}

private static byte[] StringToByteArray(string str)
{
    ASCIIEncoding enc = new ASCIIEncoding();
    return enc.GetBytes(str);
}

这将导致0xBC

但是,根据:http://www.scadacore.com/field-tools/programming-calculators/online-checksum-calculator/,这是不正确的,因为CheckSum8异或的校验和是0x31。

我到底做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2017-06-27 16:19:29

在链接的站点上,仅列出了一些16位和32位CRC,CheckSum8Xor is not a CRC。CRC0xBC循环冗余校验来自名为“-8/-S2”的8位循环冗余校验,请参阅http://reveng.sourceforge.net/crc-catalogue/1-15.htm

票数 2
EN

Stack Overflow用户

发布于 2017-06-28 03:27:58

啊,好吧,我已经过度迭代了这个校验和计算。

好吧,在这种情况下,这很简单:

代码语言:javascript
复制
public static byte Checksum8XOR(byte[] data)
        {
            byte checksum = 0x00;

            for (int i = 0; i < data.Length; i++)
            {

                checksum ^= data[i];

            }


            return checksum;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44767611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档