/// <summary> /// Url解码 /// </summary> /// <param name="str">原始字符串</param> /// <param name="isuft8">是否utf8</param> /// <returns></returns> public static string Url2Str_(string Url, bool IsUTF8 = false) { string result = string.Empty; int len = Url.Length; ; int H, L; string Hex = "0123456789ABCDEF"; List<byte> li = new List<byte>(); while (i < len) { ) != "%") { li.Add(Convert.ToByte(Url.Substring(i, ))); } else { i++; H = Hex.IndexOf(Url.Substring(i, ).ToUpper()); i++; L = Hex.IndexOf(Url.Substring(i, ).ToUpper()); li.Add(( + L)); } i++; } if (IsUTF8 == true) { result = Encoding.UTF8.GetString(li.ToArray()); } else { result = Encoding.Default.GetString(li.ToArray()); } return result; } /// <summary> /// Url编码 /// </summary> /// <param name="str">原始字符串</param> /// <param name="isuft8">是否utf8</param> /// <returns></returns> public static string Str2Url_(string strCode, bool NoEncodNum = false, bool isuft8 = false) { StringBuilder sb = new StringBuilder(); byte[] byStr; if (isuft8 == false) { byStr = System.Text.Encoding.Default.GetBytes(strCode); } else { byStr = System.Text.Encoding.UTF8.GetBytes(strCode); } System.Text.RegularExpressions.Regex regKey = new System.Text.RegularExpressions.Regex("^[A-Za-z0-9]+$"); ; i < byStr.Length; i++) { string strBy = Convert.ToChar(byStr[i]).ToString(); if (NoEncodNum == true) { if (regKey.IsMatch(strBy)) { //是字母或者数字则不进行转换 sb.Append(strBy); } else { sb.Append()); } } else { sb.Append()); } } return (sb.ToString()); }
/// <summary> /// Url编码,encoding默认为utf8编码 /// </summary> /// <param name="str">需要编码的字符串</param> /// <param name="encoding">指定编码类型</param> /// <returns>编码后的字符串</returns> public static string Str2Url(string str, Encoding encoding = null) { if (encoding == null) { return System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8); } else { return System.Web.HttpUtility.UrlEncode(str, encoding); } } /// <summary> /// Url解码,encoding默认为utf8编码 /// </summary> /// <param name="str">需要解码的字符串</param> /// <param name="encoding">指定解码类型</param> /// <returns>解码后的字符串</returns> public static string Url2Str(string str, Encoding encoding = null) { if (encoding == null) { return System.Web.HttpUtility.UrlDecode(str, Encoding.UTF8); } else { return System.Web.HttpUtility.UrlDecode(str, encoding); } }
/// <summary> ///_usc2到ansi /// </summary> /// <param name="s"></param> /// <returns></returns> public static string Unicode2StringEX(string str) { return HttpUtility.UrlDecode(Regex.Unescape(str)); } /// <summary> /// 到ansi到usc2 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string Str2Unicode(string str) { return HttpUtility.UrlEncodeUnicode(str); }
/// <summary> /// UTF8到GB2312 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string UTF82Str(string str) { string result = str; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str); result = System.Text.Encoding.Default.GetString(byteArray, , byteArray.Length); return result; } /// <summary> /// GB2312到UTF8 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string Str2UTF8(string str) { string result = str; byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); result = System.Text.Encoding.UTF8.GetString(byteArray, , byteArray.Length); return result; }
/// <summary> /// 将字符串转换为base64格式 默认UTF8编码 /// </summary> /// <param name="str">原始字符串</param> /// <param name="encoding">编码</param> /// <returns></returns> public static string String2Base64(string str, Encoding encoding = null) { if (encoding == null) { encoding = Encoding.UTF8; } return Convert.ToBase64String(encoding.GetBytes(str)); } /// <summary> /// base64字符串转换为普通格式 默认UTF8编码 /// </summary> /// <param name="str">原始字符串</param> /// <param name="encoding">编码</param> /// <returns></returns> public static string Base642String(string str, Encoding encoding = null) { if (encoding == null) { encoding = Encoding.UTF8; } byte[] buffer = Convert.FromBase64String(str); return encoding.GetString(buffer); }