字符转码
#region 字符转码 /// <summary> /// 对指定字符串进行URL标准化转码 中文字符串转换成UTF-8 /// </summary> /// <param name="text"></param> /// <param name="encoding"></param> /// <returns></returns> public static string UrlEncode(string text, Encoding encoding) { StringBuilder sb = new StringBuilder(); byte[] byData = encoding.GetBytes(text); for (int i = 0; i < byData.Length; i++) { sb.Append(@"%" + Convert.ToString(byData[i], 16)); } return sb.ToString(); } /// <summary> /// 字符串utf-8转换成GBK格式 /// </summary> /// <param name="strtxt"></param> /// <returns></returns> public static string Utf8toGBK(string strtext) { byte[] buffer = Encoding.UTF8.GetBytes(strtext); string result = Encoding.GetEncoding("GBK").GetString(buffer); return result; } /// <summary> /// 获取GBK编码 汉字转成GBK十六进制码 /// </summary> /// <returns></returns> public static string GetGBKCode(string Chars) { string Result = string.Empty; byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(Chars); foreach (byte b in gbk) { Result += "\\'" + string.Format("{0:X2}", b); } return Result; } /// <summary> /// Gb2312到UTF-8的字符编码转换 /// </summary> /// <param name="str"></param> /// <returns></returns> public string GB2312ToUTF8(string str) { Encoding uft8 = Encoding.GetEncoding(65001); Encoding gb2312 = Encoding.GetEncoding("gb2312"); byte[] temp = uft8.GetBytes(str); byte[] temp1 = Encoding.Convert(uft8, gb2312, temp); string result = gb2312.GetString(temp1); return result; } #endregion