NORTEKAS
asked on
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)
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;
[MarshalAs(UnmanagedType.U1)]
public ChannelType ChannelType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] ChannelConfig;
}
or
[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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, I'll be beggared. It was not "accepted" when I clicked on the beggar.
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"