URL中文编码

/// <summary>
        /// GB2312编码
        /// </summary>
        /// <param name="str">待编码字符</param>
        /// <returns>编码结果</returns>

  public static string GB2312(string str)
{
StringBuilder sb = new StringBuilder();
//GB2321的编码方式
byte[] byStr = System.Text.Encoding.GetEncoding ("GB2312").GetBytes(str);
for (int i = ; i < byStr.Length; i++)
{
//转换为16进制方式 可选2,8,10,16进制
sb.Append(@"%" + Convert.ToString(byStr[i], ));
}
return (sb.ToString());
}

/// <summary>
        /// GB2312解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>

 public static string DeGB2312(string str)
{
byte[] bytes = new byte[str.Split('%').Length ];
int i=;
foreach (var item in str.Split('%'))
{
if (item !="")
{
//转换为16进制的字节
bytes[i] = Convert.ToByte(item,);
i++;
} }
//GB2312的解码
return Encoding.GetEncoding ("GB2312").GetString(bytes);
}

/// <summary>
        /// UTF8编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>

 public static string EnUTF8(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = ; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], ));
} return (sb.ToString());
}

/// <summary>
        /// UTF8解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>

 public static string DeUTF8(string str)
{
byte[] bytes = new byte[str.Split('%').Length ];
int i=;
foreach (var item in str.Split('%'))
{
if (item !="")
{
bytes[i] = Convert.ToByte(item,);
i++;
} }
return Encoding.UTF8.GetString(bytes);
}

///C# 32位md5
/// <summary>
/// 获得32位的MD5加密
/// </summary>
/// <param name="str">加密字符串</param>
/// <returns>返回值</returns>

 public static string GetMD532(string str)
{
MD5 md5 = MD5.Create();
byte[] d = md5.ComputeHash(Encoding.Default.GetBytes(str));
return BitConverter.ToString(d).Replace("-", "").ToLower(); }
上一篇:Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)


下一篇:关于Java 软件工程师应该知道或掌握的技术栈