Best way to serialize binary structure

Posted on 2009-02-17
Last Modified: 2013-11-07
We have a fixed binary format that we need to handle with managed code (C#). A typical structure consist of some fixed fields, but often with a dynamic part (array of bytes or sub structs). The number of elements in the array would typically be defined in a separate field. Some of the fields are fixed strings in ansi.

I have looked at several ways to do this, but none of them seem optimal.
* define struct with StructLayout and MarshalAs attributes (does not seem to handle all necessary cases)
* define struct as class an do serialization with BinaryReader/BinaryWriter (seem to add a lot of unneccessary logic like keeping string to a fixed size)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]

    public struct Device


        public byte DeviceId;

        public byte Type;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]

        public string Name;

        public byte ChannelIdx;


        public ChannelType ChannelType;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]

        public byte[] ChannelConfig;




    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]

    public class Station : IBinarySerilizable


        byte id;

        byte type;

        UInt16 size;

        UInt16 stationId;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)] 

        public string stationName;

        UInt16 nDevices;

        Device[]    devices;

        UInt16 checksum;


Open in new window

Question by:NORTEKAS
    LVL 23

    Accepted Solution

    The "best way" is going to depend on a meaning of "good".  If you believe that what is good in life is "to crush your enemies, see them driven before you, and hear the lamentations of their women", then pretty much anything that works will do.

    Logic is only unnecessary if it is not needed.  There is a big difference here in whether you are in control of both sides - serialization and deserialization.  If you are, implementing ISerializable and controlling both sides is the most natural (and your layout when SERIALIZED doesn't matter all that much).

    If you are serializing for somebody ELSE to deserialize, and your layout needs to match that structure exactly
    then you are going to have to write each byte manually.
    LVL 51

    Expert Comment

    by:Mark Wills
    Jeez, I would have accepted that answer just for that definition of GOOD :-)  

    as for :

    (seem to add a lot of unneccessary logic like keeping string to a fixed size)  bah-humbug to the "unneccessary logic" comment. It is like (well not really) TIFF files where by the tags are meant to help define the content, but no rules on the tag combinations to the point where some TIFF images are not displayable in systems supporting TIFF.

    With binary serialization, supposedly, all necessary information that is required to create an exact binary copy of the object is saved, in fact part of the attraction of binary is that it can use that information to reconstruct "automatically", however, it is not easily portable to any other platform other than the one it was generated from.  So might depend a little on the serializable object, if you are moving across platforms where key objects will need to be presented/interpretted slightly differently, then you have very little choice, and will probably need to deserialize according to a predefined / fixed format. I can see now why you say less than optimal.

    What is it that you are doing ?

    Have you read : - the bottom paragraph mainly just for "thoughts"
    LVL 51

    Expert Comment

    by:Mark Wills
    Well, I'll be beggared. It was not "accepted" when I clicked on the beggar.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now