C# CRC校验的一点感悟

今天在鼓捣一个手持操做器的时候,遇到一点问题,记录一下今天的经验包this

因为以前公司产品在校验时基本上都是和校验,今天在准备用C#模拟一个古董操做器的时候,却遇到一个问题,模拟器发出的数据,主板一概不回复,对比通信协议也没发现什么问题。因为文档有些不全,只是知道通信格式,对比以后以为应该是校验出了问题。因为CRC校验是数据通讯领域最经常使用的校验方式,问了几个老家伙以后才知道这个四字节ASCII码校验和应该是CRC16-CCITT生成的,而后就去仔细看大学时候煞笔了好久也没明白的CRC校验的细节。spa

具体CRC如何生成我不阐述了,关键点在于“生成多项式”和初始值。code

CRC16-CCITT的生成多项式是 0x1021;blog

很少说了,提供代码CRC16-CCITT类ci

public class Crc16Ccitt
    {
        public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
        const ushort poly = 4129;
        ushort[] table = new ushort[256];
        ushort initialValue = 0;


        public Crc16Ccitt(InitialCrcValue initialValue)
        {
            this.initialValue = (ushort)initialValue;
            ushort temp, a;
            for (int i = 0; i < table.Length; ++i)
            {
                temp = 0;
                a = (ushort)(i << 8);
                for (int j = 0; j < 8; ++j)
                {
                    if (((temp ^ a) & 0x8000) != 0)
                    {
                        temp = (ushort)((temp << 1) ^ poly);
                    }
                    else
                    {
                        temp <<= 1;
                    }
                    a <<= 1;
                }
                table[i] = temp;
            }
        }

        public ushort ComputeChecksum(byte[] bytes)
        {
            ushort crc = this.initialValue;
            for (int i = 0; i < bytes.Length; ++i)
            {
                crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
            }
            return crc;
        }

        public ushort ComputeChecksum(List<byte> listTemp)
        {
            byte[] bytes = listToBytes(listTemp);
            ushort crc = this.initialValue;
            for (int i = 0; i < bytes.Length; ++i)
            {
                crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
            }
            return crc;
        }

        public byte[] ComputeChecksumBytes(byte[] bytes)
        {
            ushort crc = ComputeChecksum(bytes);
            return BitConverter.GetBytes(crc);
        }

        public byte[] listToBytes(List<byte> listTemp)
        {
            int length = listTemp.Count();
            byte[] bytes = new byte[length];
            for (int i = 0; i < length; i++)
            {
                bytes[i] = listTemp[i];
            }
            return bytes;
        }
    }

 

最后,请叫我红领巾文档

相关文章
相关标签/搜索