C# 實現十六進制Unicode編碼字符串轉換為漢字


網上找了幾個方法,但是運行之后會報錯,提示要解析的字符串格式不正確。然后我猜想可能是傳入的字符串 \u60a8\u4eca\u65e5\u5df2\u7b7e\u5230 中帶"\"的原因,加了一行     strDecode=strDecode.Replace("\\","");  把斜杠去掉,果然,解析成功了。

 

/// <summary>
        ///作用:將16進制數據編碼轉化為字符串,是Encode的逆過程
        /// </summary>
        /// <param name="strDecode"></param>
        /// <returns></returns>
        public static string DecodeHexadecimalToStr(string strDecode)
        {
            string outStr = "";

            strDecode=strDecode.Replace("\\","");
            if (!string.IsNullOrEmpty(strDecode))
            {
                string[] strlist = strDecode.Replace("/", "").Split('u');
                try
                {
                    for (int i = 1; i < strlist.Length; i++)
                    {
                        //將unicode字符轉為10進制整數,然后轉為char中文字符  
                        outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
                    }
                }
                catch (FormatException ex)
                {
                    outStr = ex.Message;
                }
            }
            return outStr;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM