Structuring for serialization / deserialization of binary messages

I am new to C # binary and am asking a question on how to do this. I have an application I'm trying to contact that has a specific binary message format. It should start with a hexadecimal code B8and end with a hexadecimal code BBwith a binary message between them. What is the best way to take a byte buffer and pass it into a message for easy access to message properties? I would introduce a structure, but to be honest, I really don't know.

EDIT:

The reason I don't want this in binary is because I can easily use the data in my application. For example, I would like to convert binary bits that represent the type of instruction to enumerate. Like this (just a representation of what I would like to do):

struct CommandMessage
{
    public CommandType Command { get; set; }
    public object Data { get; set; }
}

enum CommandType
{
    UserJoined,
    MessageReceived
}
+5
source share
1 answer

I suggest using protobuf-netfor serialization DTO.

So, define some object, for example Package( CommandMessagein your example), which has

public Command Command;

public byte[] Data;(serialized from protobuf)

Based on, Commandyou can deserialize Datafor a specific type DTOusing protobuf.

, Package. , Package / / ( ).

e.g package.WriteTo(buffer) [BB,Command,Data,B8]. package.ReadFrom()

+1

All Articles