我试图将我在byte[]
中的对象转换为对象。 我已经使用这个代码,我在网上找到了尝试:将字节[]转换为对象
object byteArrayToObject(byte[] bytes)
{
try
{
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter bf = new BinaryFormatter();
//ms.Position = 0;
return bf.Deserialize(ms,null);
}
catch
{
return null;
}
}
SerializationException:“流的末尾遇到了 完成解析之前。”
我与ms.Position = 0
行注释掉当然太试过...... bytes[]
只有8个字节长,每个字节是不是null
。
对此提出建议?
[编辑]
字节[]使用沿
void WriteToFile (std::ostream& file,T* value)
{
file.write(reinterpret_cast<char*>(value), sizeof(*T))
}
线凡值可以是许多不同类型的东西写入到从C++程序二进制文件。 我可以使用BitConverter从文件转换为某些对象,但任何BitConverter不会覆盖我不能做的。
哪些数据包含此数组中(写在编辑器)?它的格式是什么? – cdhowie 2013-02-19 16:41:50
为什么你不能像'(object)bytes'一样投射它?你想完成别的事吗? – 2013-02-19 16:42:14
您确定数据序列化是否正确? – MadSkunk 2013-02-19 16:42:51