码迷,mamicode.com
首页 > Windows程序 > 详细

C# CRC

时间:2019-12-31 18:48:09      阅读:103      评论:0      收藏:0      [点我收藏+]

标签: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 < 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;
            }
        }

C# CRC

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!