Binary Format Deserialization

158 Views Asked by At

I have two programs one is written in C and another is in C#. In the C# program, I am using BinaryFormatter().Serialize() method to serialize a data model and then send the serialized binary stream to C program through a TCP socket. Now In the C program, I want to extract the data so that I want to know the format of how BinaryFormatter().Serialize() generates the byte stream. I want to write a deserializer function in C for regenerating data serialized by BinaryFormatter().Serialize() in C#. Is there any documentation or information available for this?

1

There are 1 best solutions below

0
JonasH On

The only way to deserialize data from BinaryFormatter in C would be to invoke the .net framework and use BinaryFormatter.

But this is really not the best way to transfer data over the network. BinaryFormatter is slow, insecure, and fragile. The format might even change between two .net versions.

A much better way would be to use a serialization format intended for communication between different systems. I have used Protobuf .net with good result, and C should have some protobuf implementation. BSON, textbased formats like json/xml might also be alternatives.

If you are writing a "performance intensive" application it is probably a good idea to take a look at a few benchmarks before choosing a library, or do your own performance testing. In the benchmarks I have seen, BinaryFormatter is often the slowest alternative, so it is not a good choice if you are concerned about performance. The only reason I would ever use it is for reading data from some legacy system.