.net unicode漢字互相轉換

來源:互聯網
上載者:User

標籤: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漢字互相轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.