有人知道为什么结果没有变回去?

zzutnicholas 2008-07-12 10:07:31
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编译
...全文
55 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bindsang 2008-07-13
  • 打赏
  • 举报
回复
Unicode编码转到ASCII编码会造成字符信息丢失,特别是超出ASCII字符集的那些字符。再转回去当然不正确了
zzutnicholas 2008-07-13
  • 打赏
  • 举报
回复


高人看看我的代码有没有逻辑问题,谢谢了
yilunduyue 2008-07-13
  • 打赏
  • 举报
回复
单步调试啊!

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧