有人知道为什么结果没有变回去?
static string Covert(string str, Encoding t1, Encoding t2)
{
byte[] t1Bytes = t1.GetBytes(str);
byte[] t2Bytes = Encoding.Convert(t1, t2, t1Bytes);
char[] t2Chars = new char[t2.GetCharCount(t2Bytes, 0, t2Bytes.Length)];
t2.GetChars(t2Bytes, 0, t2Bytes.Length, t2Chars, 0);
string t2String = new string(t2Chars);
return t2String;
}
static void Main(string[] args)
{
string str = "你好!";
string str2 = Covert(str, Encoding.Unicode, Encoding.ASCII);
string str3 = Covert(str2, Encoding.ASCII, Encoding.Unicode);
Console.WriteLine(str3);
}
为啥,最后的结果不是”你好!“
求高人知道
VS2005编译
------解决方案--------------------
Unicode编码转到ASCII编码会造成字符信息丢失,特别是超出ASCII字符集的那些字符。再转回去当然不正确了