C# Socket发送接收字节数组与十六16进制 转换 函数

       //16进制字符串转字节数组   格式为 string sendMessage = "00 01 00 00 00 06 FF 05 00 64 00 00";
       //去空格 保偶数  转化
        private static byte[] HexStrTobyte(string hexString)
        {
            string hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
            return returnBytes;
        }

        //字节数组转16进制字符串   
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    //为C#中的字符串格式控制符
                    returnStr += bytes[i].ToString("X2");//ToString("X2")
                }
            }
            return returnStr;
        }
        //string str0x = BitConverter.ToString(result, 0, result.Length).Replace("-"," ");

你可能感兴趣的:(Unity)