3

I can easily specify offset of fields:

[StructLayout(LayoutKind.Explicit)]
public struct MyMarshaled
{
    [FieldOffset(0)]
    public int Version;
    [FieldOffset(3)]
    public int SubVersion;
}

but how I can specify field lengths in bit representation? For example like this:

[StructLayout(LayoutKind.Explicit)]
public struct MyMarshaled
{
    [FieldOffset(0, lenght=3)]
    public int Version;
    [FieldOffset(3, length=5)]
    public int SubVersion;
}

So, given the byte bitmap 00100001 it will print out struct with value '1' in both fields, i.e 001 is for Version, and 00001 is for SubVersion.

eocron
  • 6,885
  • 1
  • 21
  • 50
  • 1
    I don't believe it's possible, an `int` is 32bits wide, end of story. What kind of code do you expect the compiler to emit for later access to `SubVersion`? – René Vogt Sep 19 '17 at 08:00
  • I expect that bits by indexes 0,1,2 will be 001 and will be converted to integer Version, bits 3,4,5,6,7 will be 00001 and will be converted to SubVersion integer. That is what i expect from this. – eocron Sep 19 '17 at 08:04
  • [Bit fields in C#](https://stackoverflow.com/questions/14464/bit-fields-in-c-sharp) and [this answer](https://stackoverflow.com/questions/9311478/marshalling-stucts-with-bit-fields-in-c-sharp) give solutions mapping the other way to your problem. But essentially the answer is, no, there is no built-in way - you have to map the fields yourself. – Tim Rogers Sep 19 '17 at 08:09

0 Answers0