C # Reading a byte array

Ok, so I am building a server ↔ client application.

Basically, the server receives a packet containing the header [2 bytes], cryptoids [2 bytes] and data

I was thinking of creating a class to load the entire package (bytes []) into it, and then processing the package using methods inside the class. Now to the question. What would be the best approach to this? I need to be able to read Int16 Int32 String (int lenght) and probably float

Edit: looks like binary, but for byte [] as input

+3
source share
4 answers

There is a class BitConverter. Its static members take a byte array and an initial index and convert the bytes to the specified type. is that enough?

+2
source

, BinaryReader - . , BinaryReader. - , BinaryReader . , , BinaryReader.

, . , , , . BinaryReader , , .

public class MyBinaryReader : BinaryReader
{
    public MyBinaryReader(byte[] input) : base(new MemoryStream(input))
    {
    }

    public override string ReadString()
    {
         // read null-terminated string
    }
}
+2

, BinaryReader , BitConverter ; Encoding . ;)

-, . .

You can also look at the streaming API, rather than loading all of this into memory - this tends to become expensive for large messages.

0
source

Why not use a Stream like NetworkStream?

0
source

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


All Articles