zoukankan      html  css  js  c++  java
  • Unicode转换成汉字的C#解码代码

    rt 根据所具有的Unicode编码用C#语言把它转换成汉字的代码

    师傅的代码:

    public static string UnicodeToGB(string text)
            {
                System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(text, "\\\\u([\\w]{4})");
                if (mc != null && mc.Count > 0)
                {
                    foreach (System.Text.RegularExpressions.Match m2 in mc)
                    {
                        string v = m2.Value;
                        string word = v.Substring(2);
                        byte[] codes = new byte[2];
                        int code = Convert.ToInt32(word.Substring(0, 2), 16);
                        int code2 = Convert.ToInt32(word.Substring(2), 16);
                        codes[0] = (byte)code2;
                        codes[1] = (byte)code;
                        text = text.Replace(v, Encoding.Unicode.GetString(codes));
                    }
                }
                else
                {
    
                }
                return text;
            }
    

      这是以foreach来处理每个正则表达式的值并且连接起来代码

    我根据师傅的代码修改的代码,虽然简短了点但是运用的确实for循环

            public static string unicodetogb(string text)
            {
                System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(text, "\\\\u([\\w]{4})");
                string a = text.Replace("\\u", "");
                char[] arr = new char[mc.Count];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = (char)Convert.ToInt32(a.Substring(i * 4, 4), 16);
                }
                string c = new string(arr);
                return c;
            }
    

      其中 mc是通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合,它具有count属性。我是把整个Unicode代码转换成没有\u的字符串然后对其进行处理的方法。

  • 相关阅读:
    HDOJ 2095 find your present (2)
    HDOJ 2186 悼念512汶川大地震遇难同胞——一定要记住我爱你
    九度 1337 寻找最长合法括号序列
    九度 1357 疯狂地Jobdu序列
    HDOJ 1280 前m大的数
    九度 1343 城际公路网
    九度 1347 孤岛连通工程
    HDOJ 2151 Worm
    九度 1342 寻找最长合法括号序列II
    九度 1346 会员积分排序
  • 原文地址:https://www.cnblogs.com/guardianf/p/2649147.html
Copyright © 2011-2022 走看看