zoukankan      html  css  js  c++  java
  • Unicode 编码解码

    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();
          }

  • 相关阅读:
    Swift-'as?','as'用法
    Swift-'!','?'用法
    Swift-Debug下打印函数名和行数
    Swift-Swift中的全局变量和函数的创建
    Swift/Objective-C-Swift与Objective-C混用教程
    iOS-代码修改Info.plist文件
    hexo干货系列:(八)hexo文章自动隐藏侧边栏
    hexo干货系列:(七)hexo安装统计插件
    hexo干货系列:(六)hexo提交搜索引擎(百度+谷歌)
    hexo干货系列:(五)hexo添加站内搜索
  • 原文地址:https://www.cnblogs.com/change4now/p/5351159.html
Copyright © 2011-2022 走看看