为什么我得到的数组为零

发布于 2024-11-25 06:08:44 字数 498 浏览 2 评论 0原文

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卖梦商人 2024-12-02 06:08:44

当我怀疑您打算调用 Read 时,您正在调用 Write。换句话说,您将一个新创建的(因此全为零)数组写入具有序列化的MemoryStream使用Read而不是Write将从流中读入数组。

不过,调用 ToArray 会更简单:

byte[] myByte = memoryStream.ToArray();

You're calling Write when I suspect you meant to call Read. In other words, you're writing from a newly created (and thus full-of-zeroes) array to the MemoryStream which has your serialized data in. Using Read instead of Write will read into the array from the stream.

It would be simpler to call ToArray though:

byte[] myByte = memoryStream.ToArray();
哀由 2024-12-02 06:08:44

您正在将字节数组写入MemoryStream,而不是相反。将 Write 替换为 read 可以解决您眼前的问题。

但是 MemoryStream 有一个 ToArray() 方法,它已经完成了您想要的操作。没必要用这么复杂的方式来做。

You're writing the byte array into the MemoryStream, not the other way round. Replacing Write with read would fix your immediate problem.

But MemoryStream has a ToArray() method which already does what you want. No need to do it in such a complicated way.

九八野马 2024-12-02 06:08:44

原因:

  1. 你没有刷新binaryFormatter,所以memoryStream甚至没有被写入
  2. 你到底想做什么!?

如果 (1) 正确,请同时使用 memoryStreambinaryFormatter

Reasons:

  1. you didn't flush binaryFormatter so the memoryStream didn't even got written
  2. what are you trying to do anyway!?

If (1) is correct, put using around both memoryStream and binaryFormatter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文