jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
290 stars 62 forks source link

[FeatureRequest] Bit Fields from most significant bit #205

Closed bevanweiss closed 1 year ago

bevanweiss commented 1 year ago

I think I've got a problem similar to what is discussed in this issue https://github.com/jefffhaynes/BinarySerializer/issues/106

I don't appear to be able to find the way around it however.

An example of the issue is as below: image The bits on the most significant side (001) are from a BitLength=3 field that defines the overall structure of the remainder of the data (only the Logical is shown, there is also Port, Network, Symbolic, Data, DataTypeC, DataTypeE) As such I'd ideally like to be able to do:

class CipEpath
{
  [FieldOrder(0)]
  [FieldBitLength(3, BitOrder.MSB)
  public CipEpathSegmentType Type;

  [FieldOrder(1)]
  [SubType(nameof(Type), SipEpathSegmentType.Logical, typeof(CipEpathLogical)]
  public CipEpathSegment Segment;  <== this would translate the LogicalType into its various subdivisions like
}

class CipEpathLogicalSegment : CipEpathSegment
{
  [FieldOrder(0)]
  [FieldBitLength(3, BitOrder.MSB)]
  public CipEpathLogicalType Type;
  ...
}

However, the FieldBitLength appears to always be 'sliced' from the Least Significant Bit upwards, rather than the Most Significant Bit downwards. Hence something like this: [FieldBitLength(3, BitOrder.MSB) would be ideal, to reverse this (for specific fields). I don't think mixing BitOrder.MSB and BitOrder.LSB in the same byte would be needed...