C# CRC

  public static string CRC16(string cmdString)
        {
            try
            {
                //CRC寄存器
                //int CRCCode = 0;
              
                ushort crc = 0xFFFF;
                for (int i = 0; i < cmdString.Length / 2; i++)
                {
                    ushort cmdHex =(ushort)Convert.ToInt16( cmdString.Substring(i * 2, 2),16);
                    crc = (ushort)(crc ^ (cmdHex));
                    for (int j = 0; j < 8; j++)
                    {
                        crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
                    }
                }
                byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
                byte lo = (byte)(crc & 0x00FF); //低位置
                string his = Convert.ToString(hi, 16);
                string los = Convert.ToString(lo, 16);
                string res = los + his;
                return res;
             
                  
              
            }
            catch
            {
                throw;
            }
        }
原文地址:https://www.cnblogs.com/xuchanghua/p/12125694.html