zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    DIV 实现可拖拽 功能(留档)
    JS网站当前日期在IE9、Chrome和FireFox中年份显示为113年的解决方法 getFullYear();
    ASP.Net MVC C#画图 页面调用
    iOS NSDecimalNumber 货币计算 四舍五入
    [日记]寒假发生了什么
    [其他]寒假作业是什么
    [考试总结]近期第一次在下午考的一场考试
    [考试总结]不写部分分下场会很惨的一场考试
    [考试总结]毒瘤题×4的一场考试
    [考试总结]出数据变成做构造题的一场考试
  • 原文地址:https://www.cnblogs.com/jx270/p/3864391.html
Copyright © 2011-2022 走看看