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;
            }
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM