c# 字符串(含有汉字)转化为16进制编码(转)

public static string Str2Hex(string s) 
        { 
            string result = string.Empty; 
 
            byte[] arrByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);     
            for(int i = 0; i < arrByte.Length; i++) 
            { 
                result += "&#x" + System.Convert.ToString(arrByte[i], 16) + ";";        //Convert.ToString(byte, 16)把byte转化成十六进制string 
            } 
 
            return result; 
        } 
 
 
变成可以在网上传输的那种16进制编码,类似%8D%E2这种?这样的话,
用System.Web.HTTPUtility.URLEncode()就行。 
  
  光光10进制转换到16进制的话,可以用   bytes(i).ToString("X"),   
这是将一个字节转换为一个16进制字符串,"X"表示大写16进制字符,用"x"可以得到小写的。   
 
 
参考
字符串(含有汉字)转化为ascII16进制问题
http://topic.csdn.net/t/20040905/22/3342635.html
 
加码解码 
http://xiaodi.cnblogs.com/archive/2005/04/26/145493.aspx 
 
 
 
        public string EncodingSMS(string s) 
        { 
            string result = string.Empty; 
 
            byte[] arrByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);     
            for(int i = 0; i < arrByte.Length; i++) 
            { 
                result += System.Convert.ToString(arrByte[i], 16);        //Convert.ToString(byte, 16)把byte转化成十六进制string 
            } 
 
            return result; 
        } 
 
        public string DecodingSMS(string s) 
        { 
            string result = string.Empty; 
 
            byte[] arrByte = new byte[s.Length / 2]; 
            int index = 0; 
            for(int i = 0; i < s.Length; i += 2) 
            { 
                arrByte[index++] = Convert.ToByte(s.Substring(i,2),16);        //Convert.ToByte(string,16)把十六进制string转化成byte 
            } 
            result = System.Text.Encoding.Default.GetString(arrByte); 
 
            return result; 
 
        }
 
 
加码解码的规则如下: 
加码时将字符串中的所有字符转换成其对应的ASCII值的16进制值,例如:“A”的ASCII码值为65,以16进制值表示为41,故应发送两个字符“41”以代表字符“A”。 
对于汉字则以其内码的16进制值来表示,如“测试”应为:B2E2CAD4。 
 
 
原理: 
 
 
            string aaa = "AB测试"; 
            byte[] bbb = System.Text.Encoding.Default.GetBytes(aaa); 
            string ccc  = System.Text.Encoding.Default.GetString(bbb); 
 
            for(int i = 0; i < bbb.Length; i++) 
            { 
                Response.Write(System.Convert.ToString(bbb[i], 16)); 
            }                  
            Response.Write(ccc);
原文地址:https://www.cnblogs.com/jx270/p/3864391.html