码迷,mamicode.com
首页 > Web开发 > 详细

.net unicode汉字互相转换

时间:2017-04-10 14:34:00      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:sum   append   net   字符   tostring   global   itop   int   pre   

【两种方法】.net unicode汉字互相转换

=========================================


 


//汉字转Unicode编码(ASCII)

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;

        }

 

//TO汉字 srcText为去除\u之后的4位字符


using System.Globalization;



private string ToCN(string srcText)

        {

            string str = srcText;

            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), NumberStyles.HexNumber).ToString());

            return Encoding.Unicode.GetString(bytes);

        }


 

================================================================
public static string ConvertToGB(string unicodeString)  
     {  
         string[] strArray = unicodeString.Split(new string[] { @"u" }, StringSplitOptions.None);  
         string result = string.Empty;  
         for (int i = 0; i < strArray.Length; i++)  
         {  
             if (strArray[i].Trim() == "" || strArray[i].Length < 2 || strArray.Length <= 1)  
             {  
                 result += i == 0 ? strArray[i] : @"u" + strArray[i]; continue;  
             }   
             for (int j = strArray[i].Length > 4 ? 4 : strArray[i].Length; j >= 2; j--)  
             {  
                 try  
                 {  
                     result += char.ConvertFromUtf32(Convert.ToInt32(strArray[i].Substring(0, j), 16)) + strArray[i].Substring(j);  
                     break;  
                 }  
                 catch  
                 {  
                     continue;  
                 }  
             }  
         } return result;  
     }  
  
     public static string ConvertToUnicode(string strGB)  
     {  
         char[] chs = strGB.ToCharArray(); string result = string.Empty; foreach (char c in chs)  
         {  
             result += @"u" + char.ConvertToUtf32(c.ToString(), 0).ToString("x");  
         }  
         return result;  
     }

 

/// <summary>
    /// 普通字符串与UniCode字符串间转换
    /// </summary>
    public static class StringToUniCode
    {
        /// <summary>
        /// 字符串转为UniCode码字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string StringToUnicode(string s)
        {
            char[] charbuffers = s.ToCharArray();
            byte[] buffer;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < charbuffers.Length; i++)
            {
                buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString());
                sb.Append(String.Format("//u{0:X2}{1:X2}", buffer[1], buffer[0]));
            }
            return sb.ToString();
        }
        /// <summary>
        /// Unicode字符串转为正常字符串
        /// </summary>
        /// <param name="srcText"></param>
        /// <returns></returns>
        public static 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;
        }
    }

 

.net unicode汉字互相转换

标签:sum   append   net   字符   tostring   global   itop   int   pre   

原文地址:http://www.cnblogs.com/liuxiaoji/p/6688767.html

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