码迷,mamicode.com
首页 > 其他好文 > 详细

字符转码

时间:2014-05-26 00:33:03      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

bubuko.com,布布扣
     #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
bubuko.com,布布扣

 

字符转码,布布扣,bubuko.com

字符转码

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/Warmsunshine/p/3750068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!