为什么我得到的数组为零
myByte - 所有字节均为零
a = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
var memoryStream = new MemoryStream();
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, m_workspace.ListPlatforms.ToArray());
myByte = new byte[memoryStream.Length];
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.Write(myByte, 0, myByte.Length);
string a = System.Convert.ToBase64String(myByte);
可能是什么原因
myByte - all bytes are zero
a = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
var memoryStream = new MemoryStream();
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, m_workspace.ListPlatforms.ToArray());
myByte = new byte[memoryStream.Length];
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.Write(myByte, 0, myByte.Length);
string a = System.Convert.ToBase64String(myByte);
what could be the reason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我怀疑您打算调用
Read
时,您正在调用Write
。换句话说,您将从一个新创建的(因此全为零)数组写入具有序列化的MemoryStream
使用Read
而不是Write
将从流中读入数组。不过,调用
ToArray
会更简单:You're calling
Write
when I suspect you meant to callRead
. In other words, you're writing from a newly created (and thus full-of-zeroes) array to theMemoryStream
which has your serialized data in. UsingRead
instead ofWrite
will read into the array from the stream.It would be simpler to call
ToArray
though:您正在将字节数组写入
MemoryStream
,而不是相反。将Write
替换为 read 可以解决您眼前的问题。但是
MemoryStream
有一个ToArray()
方法,它已经完成了您想要的操作。没必要用这么复杂的方式来做。You're writing the byte array into the
MemoryStream
, not the other way round. ReplacingWrite
with read would fix your immediate problem.But
MemoryStream
has aToArray()
method which already does what you want. No need to do it in such a complicated way.原因:
binaryFormatter
,所以memoryStream
甚至没有被写入如果 (1) 正确,请同时使用
memoryStream
和binaryFormatter
。Reasons:
binaryFormatter
so thememoryStream
didn't even got writtenIf (1) is correct, put using around both
memoryStream
andbinaryFormatter
.