zoukankan      html  css  js  c++  java
  • Unicode互相转换方法

    C#字串与Unicode互相转换方法和代码:

    /// <summary>
            /// 将原始字串转换为unicode,格式为\u....\u....
            /// </summary>
            /// <param name="srcText"></param>
            /// <returns></returns>
            private string StringToUnicode(string srcText)
            {
                string dst = "";
                char[] src = srcText.ToCharArray();
                for (int i = 0; i < src.Length; i++)
                {
                    byte[] bytes = Encoding.Unicode.GetBytes(src[i].ToString());
                    string str = @"\u" + bytes[1].ToString("X2") + bytes[0].ToString("X2");
                    dst += str;
                }
                return dst;
            }

            /// <summary>
            /// 将Unicode字串\u....\u....格式字串转换为原始字符串
            /// </summary>
            /// <param name="srcText"></param>
            /// <returns></returns>
            private string UnicodeToString(string srcText)
            {
                string dst = "";
                string src = srcText;
                int len = srcText.Length / 6;
               
                for (int i = 0; i <= len - 1; i++)
                {
                    string str = "";
                    str = src.Substring(0, 6).Substring(2);
                    src = src.Substring(6);
                    byte[] bytes = new byte[2];
                    bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
                    bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
                    dst += Encoding.Unicode.GetString(bytes);
                }
                return dst;
            }

  • 相关阅读:
    C++学习之路:纯虚函数
    C++学习之路:虚函数与多态
    C++学习之路: 虚继承的内存的模型
    grep命令与正则表达式
    C++学习:lambda表达式入门
    wxWidgets在LINUX下的安装和demos的编译
    C++学习之路: STL探索之 reverse_iterator 的一个小陷阱
    JAVA入门:http基础
    sql入门
    C++学习之路: 构造函数详解与初始化列表
  • 原文地址:https://www.cnblogs.com/goooto/p/1413359.html
Copyright © 2011-2022 走看看