如何把16进制数转化为byte[]数组?

您所在的位置:网站首页 字节数组转为十六进制 如何把16进制数转化为byte[]数组?

如何把16进制数转化为byte[]数组?

2024-07-14 11:11| 来源: 网络整理| 查看: 265

///         /// 发送开锁指令        ///         ///         public void OpenLock(int mbIndex, int lockIndex)        {            OpenPort();            string command = IntToHex_1_to_24(mbIndex) + "0500" + IntToHex_1_to_24(lockIndex) + "FF00";            //LRC            int sum = 0;            for (int i = 0; i < command.Length / 2; i++)            {                string val = command.Substring(i * 2, 2);                sum += int.Parse(val, System.Globalization.NumberStyles.HexNumber);            }            byte result = (byte)((~sum) + 1);            command += result.ToString("X2");            command = ((char)0x3A).ToString() + command + ((char)0x0D).ToString() + ((char)0x0A).ToString();            byte[] buffer = Encoding.ASCII.GetBytes(command);            try            {                this.serialPort.Write(command);            }            finally            {                string hex_cmd = string.Empty;                foreach (byte b in buffer)                {                    hex_cmd += Convert.ToString(b, 16).ToUpper().PadLeft(2, '0');                }                Com.DataCool.DotNetExpand.LogHelper.Info(command + "," + hex_cmd);                if (!CurrCommandList.Contains(hex_cmd))                    CurrCommandList.Add(hex_cmd);            }        }

不用意思以前的代码没有调试过,误导你了。你看看这段代码。写串口的是十六进制的字符 ,像你这样只需要把“H”去掉就直接发给串口就行了。serialPort.Write(“H23H33H30H47H45”.Replace("H",""));



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3