日期:2014-05-18 浏览次数:21541 次
private string ToUnicodeString(string s) { StringBuilder sb = new StringBuilder(); byte[] bytes = Encoding.Unicode.GetBytes(s); for (int i = 0; i < bytes.Length; ) { sb.Append("\\u"); sb.Append(bytes[i + 1].ToString("x2")); sb.Append(bytes[i].ToString("x2")); i += 2; } return sb.ToString(); } private string ToNormalString(string input) { string pattern = "\\\\u([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})"; Match match = System.Text.RegularExpressions.Regex.Match(input,pattern); System.Collections.ArrayList numlist = new System.Collections.ArrayList(); while(match.Success) { string s = match.Groups[2].Value; byte num = byte.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier); numlist.Add(num); s = match.Groups[1].Value; num = byte.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier); numlist.Add(num); match = match.NextMatch(); } byte[] bytes = new byte[numlist.Count]; numlist.CopyTo(bytes); string result = Encoding.Unicode.GetString(bytes); return result; }
------解决方案--------------------
需要先知道对方的编码方式,然后才能使用Encoding的GetString方式解码:
string s=Encoding.Default.GetString(buffer);