转码方法:
C#:
string a = "\u5929\u6cf0\u56fd\u9645";
string b = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(a.ToCharArray()));
JAVA:
String str2 = new String(str1.getBytes("utf8"));
更正:上述的"\u5929\u6cf0\u56fd\u9645"本身就是unicode的编码,所以并不需要转码,\u开头就代表了是unicode编码,与字面常量'天泰国际'完全等价。
把"59296cf056fd9645"这样的十六进制字符串转换成unicode:
public string convertUtf8(string code)
{
byte[] chars = new byte[code.Length / 2];
Char[] cs = code.ToCharArray();
for (int i = 0; i < code.Length / 2; i++)
{
if(i%2==0)
chars[i+1] = Convert.ToByte(code.Substring(2*i,2),16);
else
chars[i - 1] = Convert.ToByte(code.Substring(2 * i, 2), 16);
}
for (int i = 0; i < chars.Length; i++) Console.WriteLine(chars[i]);
return Encoding.Unicode.GetString(chars);
}
注意Unicode的大小端,这里的unicode是小端方式的,所以例如5929在byte数组中29应该在59之前。