容易混乱的几个编码函数


1.百度编码、解码

你在百度中输入"彭成刚",点击搜索后URL栏中会变成"%C5%ED%B3%C9%B8%D5",这是怎么编码的,如何解码.下面两段代码能解决这两个疑问:

//百度编码
Console.WriteLine(System.Web.HttpUtility.UrlEncode("彭成刚",   System.Text.UnicodeEncoding.GetEncoding("GB2312")).ToUpper());

//百度解码 
Console.WriteLine(System.Web.HttpUtility.UrlDecode("%C5%ED%B3%C9%B8%D5",   System.Text.UnicodeEncoding.GetEncoding("GB2312")));

2.HTML编码、解码函数

//HTML编码
System.Web.HttpUtility.HtmlEncode(string); Server.HtmlEncode(string)
//HTML解码
System.Web.HttpUtility.HtmlDecode(string);  Server.HtmlDecode(string)

即对HTML标签编码解码,非HTML标签不对其解码 比如
--->

3.URL中编码、解码

//URL编码
System.Web.HttpUtility.UrlEncode(string); Server.UrlEncode(string);
System.Web.HttpUtility.UrlPathEncode(string); Server.UrlPathEncode(string);

UrlEncode与UrlPathEncode编码获范不一样
比如: UrlEncode对整个URL进行编码(即http://www.zzcn.net/全部编码)
UrlPathEncode对http://之后的内容进行编码(即www.zzcn.net)

//URL解码
System.Web.HttpUtility.UrlDecode(string); Server.UrlDecode(string);

4.UTF-8,GB2312转换

    utf-8>>gb2312

    string   content = "彭成刚";  
    byte[]   conby   =   Encoding.UTF8.GetBytes(content);  
    content   =   Encoding.GetEncoding("gb2312").GetString(conby);
    Console.WriteLine(content);

反之就不写啦,希望对大家有进一步认识,不足的请指正补充,谢谢

---------------------------------------------
生活的意义并不是与他人争高下,而在于享受努力实现目标的过程,结果是对自己行动的嘉奖。
↑面的话,越看越不痛快,应该这么说:

生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
原文地址:https://www.cnblogs.com/pengchenggang/p/941877.html