c#中编码与解码的奇怪现象??
最近做一个关于用C#实现一个加密算法。遇到了一个很大的麻烦,找了几天终于发现了错误的根源,但是我没有办法解释,所以也没有办法解决啊!说简单一点。就是下面的这段代码。将byte数组TestByte先用unicode格式编码,然后又用unicode格式解码,按理说,解码的结果应该和TestByte数组一样,可结果却不是这样的,问题出在倒数第2个字符编码上面。也及时数组的倒数第3、4个数字上面。可以运行看一下。实在不知道是为什么啊?请高手指点 using System;
using System.Text;
public class SamplesEncoding {
public static void Main()
{
Encoding UTF=Encoding .Unicode ;
UnicodeEncoding unicode=new UnicodeEncoding ();
byte[] TestByte= {32,37,15,83,194,117,17,168,33,81,52,41,68,155,178,19,49,7,61,220,255,84};
string str=unicode.GetString(TestByte);
byte [] test=UTF .GetBytes (str );
foreach (byte i in TestByte )
{
Console .Write ("{0},",i );
}
Console .WriteLine ();
foreach (byte i in test )
{
Console .Write ("{0},",i );
}
Console .ReadKey ();
}
}
运行结果如下:
32,37,15,83,194,117,17,168,33,81,52,41,68,155,178,19,49,7,61,220,255,84,
32,37,15,83,194,117,17,168,33,81,52,41,68,155,178,19,49,7,253,255,255,84,
[61,220]与[253,255]不一样啊??