c# - 来自具有不同类型数据的字节数组的 MemoryStream

标签 c# floating-point byte memorystream

我想创建一个包含 int32、int16 和单个值的内存流。使用 binarywriter 是没用的,所以我尝试制作字节数组。因为值有不同的类型,所以我不知道如何正确地做。所以我尝试这样做:

byte[] tab = new byte[]{2,0,0,0,3,0,3,0} - 2 是 int32(四个字节),另外两个 3 是 int16 (两个字节)

工作正常,但是当我想添加一些单个值时,它会产生错误。我不能那样做:

byte[] tab = new byte[]{2,0,0,0,3,0,3,0,4.4f,5.6f}

我必须有正确格式的流,因为流将在这个方法中读取:

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];
        }

有人知道怎么做吗??

最佳答案

与您的原始声明相反,BinaryWriter 正是您想要的。这就是它的设计目的。特别是,如果您稍后要使用 BinaryReader,它就非常合适。

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

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();
}

这会生成一个包含以下数据的字节数组:

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

需要注意的一点 - 您的写作描述写入了这些值:

- Int32
- Int16
- Int16
- Single
- Single

...但是您的阅读代码将显示为:

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

换句话说,如果您之前使用 BinaryWriter 的尝试失败了,因为它们看起来像我的初始代码,那是因为您忘记了

writer.Write(2);

写完 Int16 值后,说明存在多少个 Single 值。

请注意,如果您不需要字节数组形式的值,则无需调用 ToArray - 只需返回流(无需处理)。但是,您需要在阅读之前“倒回”它。例如:

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;
}

关于c# - 来自具有不同类型数据的字节数组的 MemoryStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4966595/

相关文章:

c - 如何确定 float 尾数的最大正基数 10 值?

java - 为什么 Double.NaN==Double.NaN 返回 false?

c# - MVVM Light Toolkit - Messenger 使用事件聚合器或调解器模式?

c# - 如何统一创建可横向滚动的二维地形?

c# - 部分 xml 序列化/反序列化

c# - 在 Python 中使用 System.Collections.Generic.List`1[System.Byte]

c++ - 函数返回负值

Python和Arduino串口,解码问题

C - 剥离最低有效字节

python - 如何将字节对象转换为 python 中的十进制或二进制表示形式?