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

Unicode 编码解码

时间:2016-04-04 01:19:27      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

1. Regex.Unescape(str);返回Unicode解码,非Unicode直接返回

/// <summary>
      /// 2.转为Unicode编码
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
  public static  string ToUnicode(string str)
    {
        var  strResult = new StringBuilder();
      if (string.IsNullOrEmpty(str)) return strResult.ToString();
      foreach (var  t in str)
      {
          strResult.Append("\\u");
          strResult.Append(((int)t).ToString("x"));
      }
      return strResult.ToString();
    }
      /// <summary>
      /// 3.Unicode 解码
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
      public static string EnUnicode(string str)
      {
          var  strResult = new StringBuilder();
          if (!string.IsNullOrEmpty(str))
          {
              string[] strlist = str.Replace("\\", "").Split(‘u‘);
              try
              {
                  for (int i = 1; i < strlist.Length; i++)
                  {
                      int charCode = Convert.ToInt32(strlist[i], 16);
                      strResult.Append((char)charCode);
                  }
              }
              catch (FormatException ex)
              {
                  return Regex.Unescape(str);
              }
          }
          return strResult.ToString();
      }

Unicode 编码解码

标签:

原文地址:http://www.cnblogs.com/change4now/p/5351159.html

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