首页 > 学院 > 开发设计 > 正文

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

2019-11-17 03:01:15
字体:
来源:转载
供稿:网友
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"),   
<PRe style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表