I'm stuck on this one.
I'm reading a binary file which has te following format:
Field name Size in bytes Example
-------------------------------------
Date 19 1998_12_22 PM 20:15
Serial 4 0001
Using the following struct and using the answers to this question I'm trying to read the file.
[StructLayout(LayoutKind.Explicit, Size=23, Pack = 1)]
struct MeasurementStruct
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 19)]
[FieldOffset(0)]
public string Date;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
[FieldOffset(19)]
public string Serial;
}
However, when instantiating the class that holds this struct I get an error at FieldOffset 19. Unfortunately, this error is in Dutch but it roughly translates to "Cannot load MeasurementStruct because field at off 19 is not aligned well (fields may overlap)".
I found out that changing the FieldOffset[19] to FieldOffset[20] makes the error disappear. However, 20 is not the right offset in my case, is it?