字符串十六进制转ASCII码

  /// 
        /// 将一条十六进制字符串转换为ASCII
        /// 
        /// 一条十六进制字符串
        /// 返回一条ASCII码
        public static string HexStringToASCII(string hexstring)
        {
            string result = Regex.Replace(hexstring, @".{2}", "$0 ");
            byte[] bt = HexStringToBinary(result);
            string lin = "";
            for (int i = 0; i < bt.Length; i++)
            {
                lin = lin + bt[i] + " ";
            }


            string[] ss = lin.Trim().Split(new char[] { ' ' });
            char[] c = new char[ss.Length];
            int a;
            for (int i = 0; i < c.Length; i++)
            {
                a = Convert.ToInt32(ss[i]);
                c[i] = Convert.ToChar(a);
            }

            string b = new string(c);
            return b;
}

 /// 
        /// 16进制字符串转换为二进制数组
        /// 
        /// 用空格切割字符串
        /// 返回一个二进制字符串
        public static byte[] HexStringToBinary(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#)