就如同浮点数转换会出现精度丢失一样,字节与字符串的转换也会出现自作聪明的结局,虽然看起来一样,但未必 是你喜欢的那个人了
@@@codevar bs = new byte[16]
{
120,
104,
0,
0,
0,
0,
0,
0,
32,
65,
57,
16,
132,
54,
151,
87
};
string s = Encoding.UTF8.GetString(bs);
var cs = Encoding.UTF8.GetBytes(s);
Console.WriteLine(s);
Console.WriteLine(Encoding.UTF8.GetString(cs));
Console.WriteLine(BitConverter.ToString(bs).Replace("-"," "));
Console.WriteLine(BitConverter.ToString(cs).Replace("-", " "));
Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(Encoding.ASCII.GetString(bs))).Replace("-", " "));
@@@
输出结果
@@@codexh?????? A9?6?W
xh?????? A9?6?W
78 68 00 00 00 00 00 00 20 41 39 10 84 36 97 57
78 68 00 00 00 00 00 00 20 41 39 10 EF BF BD 36 EF BF BD 57
78 68 00 00 00 00 00 00 20 41 39 10 3F 36 3F 57
@@@