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;
        }

 

posted @ 2020-11-02 10:34  超难微猫  阅读(2744)  评论(0编辑  收藏  举报