首页 > Windows开发 > 详细

C# CRC

时间:2019-12-31 18:24:45      阅读:97      评论:0      收藏:0      [点我收藏+]
  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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!