MemoryStream from an array of bytes with various data types

I want to create a memory stream that contains int32, int16, single values. Using binarywriter is useless, so I tried to create an array of bytes. Since the values ​​are of different types, I do not know how to do this correctly. So I am trying to do this:

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

works fine, but when I want to add some values, it generates errors. I can not do this:

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

I must have the stream in the correct format, because this 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];
        }

Does anyone have an idea how to do this?

+3
source share
3 answers

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, , .

: , 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;
}
+5

A BinaryWriter . memystream :

MemoryStream m = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(m)) {
  writer.Write(2); // count
  writer.Write((short)3);
  writer.Write((short)3);
  writer.Write(2); // count
  writer.Write(4.4f);
  writer.Write(5.6f);
}
byte[] tab = m.ToArray();

, float. , , , .

, . :

Console.WriteLine(rawData.Length);
foreach (short x in rawData) Console.WriteLine(x);
Console.WriteLine(modulusData.Length);
foreach (float f in modulusData) Console.WriteLine(f);

:

2
3
3
2
4,4
5,6
+3

, Int32 Int16 byte, , , .

- , , BinaryWriter (. )

0
source

Source: https://habr.com/ru/post/1791422/


All Articles