本文介绍了MemoryStream from bytes array with different types of data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个包含INT32,INT16,单值的内存流。使用的BinaryWriter是没用的,所以我试图使字节数组。因为值是不同类型的,我不知道如何正确地做到这一点。所以我尝试做这样的:

I want to create a memory stream which contains int32, int16, single values. Using binarywriter is useless so i tried to make bytes array. Because values are in different types, I don't know how to do it properly. So I try do like that:

字节[]选项卡=新的字节[] {2,0,0,0,3,0, 3,0} - 2 INT32(四个字节),另外两个3 INT16(两个字节)

byte[] tab = new byte[]{2,0,0,0,3,0,3,0} - 2 is int32 (four bytes), another two 3 are int16 (two bytes)

这工作得很好,但是当我想添加一些单值,它会产生错误。我不能做这样的:

that works fine, but when i want to add some single values, it generates errors. I cant do like that :

字节[]选项卡=新的字节[] {2,0,0,0,3,0,3 ,0,4.4f,5.6f}

我必须在正确的格式流,导致该流将在这个方法来读取:

I must have stream in proper format, cause that stream will be read in this method :

short[] rawData;
 float[] modulusData;
   public void rawData(Stream s)
        {

            BinaryReader br = new BinaryReader(s);

            int dataCount = br.ReadInt32();

            if (dataCount > 0)
            {
                rawData = new short[dataCount];
                for (int i = 0; i < dataCount; i++)
                    rawData[i] = br.ReadInt16();
            }
            else
                rawData = new short[0];

            dataCount = br.ReadInt32();

            if (dataCount > 0)
            {
                modulusData = new float[dataCount];
                for (int i = 0; i < dataCount; i++)
                    modulusData[i] = br.ReadSingle();
            }
            else
                modulusData = new float[0];
        }



任何人有想法如何做到这一点?

Anybody has idea how to do that ??

推荐答案

出乎你原来的说法,的BinaryWriter 是的究竟的你想要什么。这就是它的设计。特别是,它正是合适的,如果你打算 BinaryReader 以备后用。

Contrary to your original statement, BinaryWriter is exactly what you want. That's what it's designed for. In particular, it's exactly appropriate if you're going to use BinaryReader later.

您还没有说明为什么你不想使用它,但它确实是你应该用什么:

You haven't stated why you don't want to use it, but it really is what you should use:

using (MemoryStream stream = new MemoryStream())
{
    using (BinaryWriter writer = new BinaryWriter(stream))
    {
        writer.Write(2);
        writer.Write((short) 3);
        writer.Write((short) 3);
        writer.Write(4.4f);
        writer.Write(5.6f);
    }
    byte[] bytes = stream.ToArray();
}

这产生具有下列数据的字节数组:

This produces a byte array with the following data:

[Int32    ] [Int16] [Int16] [Single   ] [Single   ]
02 00 00 00 03 00   03 00   CD CC 8C 40 33 33 B3 40

要注意的一点 - 你的的说明写道:这些值

One point to note - your writing description writes these values:

- Int32
- Int16
- Int16
- Single
- Single

...但你的阅读的代码将改为:

... but your reading code will read:

- Int32 (value 2)
- Int16
- Int16
- Int32 (this wasn't written - so you're reading data from the first Single!)
- ???

在换句话说,如果你以前的尝试与的BinaryWriter 中失败,因为他们看起来像我最初的代码,那是因为你忘了。

In other words, if your previous attempts with BinaryWriter were failing because they looked like my initial code, it's because you forgot a

writer.Write(2);



写的Int16值之后,说单身值多少人出席。

after writing the Int16 values, to say how many Single values were present.

请注意,如果你不需要的值作为一个字节数组,你不需要调用的ToArray - 只返回流(不设置它)。但是,你要阅读它之前,倒带吧。例如:

Note that if you don't need the values as a byte array, you don't need to call ToArray - just return the stream (without disposing of it). However, you'll want to "rewind" it before reading it. For example:

public Stream GetData()
{
    MemoryStream stream = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(stream); // Don't close at the end!
    writer.Write(2);
    writer.Write((short) 3);
    writer.Write((short) 3);
    writer.Write(2); // Extra count for the Single values
    writer.Write(4.4f);
    writer.Write(5.6f);
    writer.Flush(); // May not be required...

    stream.Position = 0; // Rewind so stream can be read again
    return stream;
}

这篇关于MemoryStream from bytes array with different types of data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:44