谷歌半天。也没找到C#版本的CRC16校验源码。至少我没用明白。我就是想把16进制数据传进去,然后给返回高字节,低字节,就OK。怎么弄啊?
看网上好多应该都是正确的,就是不会用啊。

解决方案 »

  1.   

    byte[] crcCit = new byte[2];// 接收到字节数组
     byte[] byteMessage = new byte[16] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
           
    byte[] byteTime = new byte[] { byteMessage[0], byteMessage[1] ......};
     crcCit = this.GetCRCCcitt(byteTime); // 校验位的判断
    if ((crcCit[0] == XXXXX) && (crcCit[1] == ????????))
    {
        ...............
    }      
           /// <summary>
            /// CRC16-CCITT检验码的取得
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public byte[] GetCRCCcitt(byte[] data)
            {
                long crc = 0;
                byte i = 0x80;
                int j = 0;            // 高位字节
                string crch;
                // 低位字节
                string crcl;            byte[] bytes = new byte[2];             for (j = 0; j < data.Length; j++)
                {
                    i = 0x80;                while (i != 0)
                    {
                        if ((crc & 0x8000) != 0)
                        {
                            crc = crc * 2;
                            crc = crc ^ 0x1021;
                        }
                        else
                        {
                            crc = crc * 2;
                        }                    if ((data[j] & i) != 0)
                        {
                            crc = crc ^ 0x1021;
                        }                    i = Convert.ToByte(i / 2);                    if (crc > 65536)
                        {
                            crc = crc - 65536;
                        }
                    }
                }            // 取得高位字节
                int intCrc = Convert.ToByte(crc / 256);
                crch = Convert.ToString(intCrc, 16);
                if (crch.Length == 1)
                {
                    crch = "0" + crch;
                }            // 取得低位字节
                crcl = Convert.ToString(crc % 256, 16);
                if (crcl.Length == 1)
                {
                    crcl = "0" + crcl;
                }            bytes[0] = (byte)Convert.ToByte(crch.Substring(0, 2), 16);
                bytes[1] = (byte)Convert.ToByte(crcl.Substring(0, 2), 16);            return bytes;
            }
      

  2.   

    楼上仁兄的我没哟个明白啊。比如我要求“05 05 05 05”的CRC16校验码等于多少。怎么做?
    求赐教