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