• URL中文编码


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

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


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

     1 public static string DeGB2312(string str)
     2         {
     3             byte[] bytes = new byte[str.Split('%').Length ];
     4             int i=0;
     5             foreach (var item in str.Split('%'))
     6             {
     7                 if (item !="")
     8                 {
     9                     //转换为16进制的字节
    10                     bytes[i] = Convert.ToByte(item,16);
    11                     i++;
    12                 }
    13               
    14             }
    15             //GB2312的解码
    16             return Encoding.GetEncoding ("GB2312").GetString(bytes);
    17         }
    View Code



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

     1 public static string EnUTF8(string str)
     2         {
     3             StringBuilder sb = new StringBuilder();
     4             byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
     5             for (int i = 0; i < byStr.Length; i++)
     6             {
     7                 sb.Append(@"%" + Convert.ToString(byStr[i], 16));
     8             }
     9 
    10             return (sb.ToString());
    11         }
    View Code


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

     1 public static string DeUTF8(string str)
     2         {
     3             byte[] bytes = new byte[str.Split('%').Length ];
     4             int i=0;
     5             foreach (var item in str.Split('%'))
     6             {
     7                 if (item !="")
     8                 {
     9                     bytes[i] = Convert.ToByte(item,16);
    10                     i++;
    11                 }
    12               
    13             }
    14             return Encoding.UTF8.GetString(bytes);
    15         }
    View Code

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

    1 public static string GetMD532(string str)
    2 {
    3     MD5 md5 = MD5.Create();
    4     byte[] d = md5.ComputeHash(Encoding.Default.GetBytes(str));
    5     return BitConverter.ToString(d).Replace("-", "").ToLower();
    6 
    7 }
    View Code
  • 相关阅读:
    [C#] 多线程总结(结合进度条)
    [Winform] DataGridView 中 DataGridViewComboBox 的可编辑
    [Tool] 仿博客园插入代码的 WLW 插件
    [C#] 获取打印机列表
    [Access] C# 通过 COM 组件访问 Access 文件
    [Excel] 打印设置
    [Q&A] 应用程序清单生成失败
    [Excel] Worksheet.PasteSpecial
    canvas裁剪图片,纯前端
    javascript将base64编码的图片数据转换为file并提交
  • 原文地址:https://www.cnblogs.com/2013likong/p/3427960.html
Copyright © 2020-2023  润新知