当我尝试将对象转换为字节数组时,我得到了一个奇怪的数组。这是代码:
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
Console.WriteLine(ByteArrayToString(ms.ToArray()));
}
//int obj = 50;
//string ByteArrayToString(byte[] byteArr) the functionality of this method is pretty obvious
结果是这样的:
"00 01 00 00 00 FF FF FF FF 01 00 00 00 00 00 00 00 04 01 00 00 00 0C 53 79 73 74 65 6D 2E 49 6E 74 33 32 01 00 00 00 07 6D 5F 6560C 07 6D 5F 6560C 06 32 00 00 00 0B "
有人可以向我解释为什么吗?:) 最佳结果应该只有“32 00 00 00”。