C# CRC

2021-01-20 16:15

阅读:384

标签:crc   static   catch   convert   nbsp   turn   for   his   str   

  public static string CRC16(string cmdString)
        {
            try
            {
                //CRC寄存器
                //int CRCCode = 0;
              
                ushort crc = 0xFFFF;
                for (int i = 0; i                 {
                    ushort cmdHex =(ushort)Convert.ToInt16( cmdString.Substring(i * 2, 2),16);
                    crc = (ushort)(crc ^ (cmdHex));
                    for (int j = 0; 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;
            }
        }

C# CRC

标签:crc   static   catch   convert   nbsp   turn   for   his   str   

原文地址:https://www.cnblogs.com/xuchanghua/p/12125694.html


评论


亲,登录后才可以留言!