We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Best way to serialize binary structure

Medium Priority
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

Watch Question

Test Developer/Validator
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018

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 Advisor, Page Editor
Distinguished Expert 2018

Well, I'll be beggared. It was not "accepted" when I clicked on the beggar.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.