C#中16进制字节数组与字符串的相互转换代码

        /// 
        /// 将16禁进制字节数组连接成字符串
        /// 
        /// 
        public static string HexConnToString(byte[] hexByte)
        {
            string hexString = "";
            for (byte i = 0; i < hexByte.Length; i++)
            {
                hexString += hexByte[i].ToString("X2");
                if (i < hexByte.Length - 1)
                    hexString += " ";
            }
            return hexString;
        }

        /// 
        /// 16进制字节数组字符串 解析成16进制字节数组
        /// 
        /// 
        /// 
        public static byte[] HexStringToByteArray(string hexstring)
        {
            string[] tmpary = hexstring.Trim().Split(' ');
            byte[] buff = new byte[tmpary.Length];
            for (int i = 0; i < buff.Length; i++)
                buff[i] = Convert.ToByte(tmpary[i], 16);
            return buff;
        }

你可能感兴趣的:(c#,算法)