Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2988
  • Last Modified:

Best way to serialize binary structure

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

  • 2
1 Solution
Jens FiedererCommented:
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.
Mark WillsTopic AdvisorCommented:
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 : http://msdn.microsoft.com/en-us/library/ms973893.aspx - the bottom paragraph mainly just for "thoughts"
Mark WillsTopic AdvisorCommented:
Well, I'll be beggared. It was not "accepted" when I clicked on the beggar.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now