網上找了幾個方法,但是運行之后會報錯,提示要解析的字符串格式不正確。然后我猜想可能是傳入的字符串 \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; }
