5

I'm trying to write an interface for communicating with a network protocol, but the IEEE document describes the protocol at the bit level with information split accross a single byte.

What would be the best way to go about handling a C typedef such as

typedef struct {
   Nibble transportSpecific;
   Enumeration4 messageType;
   UInteger4 versionPTP;
   UInteger16 messageLength;
   UInteger8 domainNumber;
   Octet flagField[2];
   Integer64 correctionfield;
   PortIdentity sourcePortIdentity;
   UInteger16 sequenceId;
   UInteger8 controlField;
   Integer8 logMessageInterval;
} MsgHeader;

when porting a compatibility layer to .Net?

Greg Buehler
  • 3,897
  • 3
  • 32
  • 39
  • I don't think you can get data types that small in dotnet... You might have to create a message-passing program in C to actually transform the dotnet values down into something your *other* C program can see... – Frosty840 Oct 07 '10 at 15:02
  • 2
    @Frosty: `[FieldOffset]` – SLaks Oct 07 '10 at 17:24
  • and @SLaks does it once again. Is there anything you don't know how to wrangle in C# my friend? – jcolebrand Nov 01 '10 at 17:07
  • @drachenstern: Unfortunately, yes. http://stackoverflow.com/questions/263276/change-keyboard-layout-for-other-process and http://stackoverflow.com/questions/2134391/create-an-interactive-logon-session (in Vista). – SLaks Nov 01 '10 at 17:07
  • lol, I know I know ... but you're always so on-target and fast. – jcolebrand Nov 01 '10 at 17:08

1 Answers1

1

FieldOffsetAttribute may be of help to you, although there is no way to represent values smaller than a byte.

I would use a byte to represent the two values for interop purposes, and then access the value via property getters.

unsafe struct MsgHeader
{
    public Nibble transportSpecific;
    //Enumeration4 messageType;
    //UInteger4 versionPTP;
    // use byte as place holder for these two fields
    public byte union;
    public ushort messageLength;
    public byte domainNumber;
    public fixed byte flagField[2];
    public long correctionfield;
    public PortIdentity sourcePortIdentity;
    public ushort sequenceId;
    public byte controlField;
    public sbyte logMessageInterval;

    // access value of two fields via getters
    public byte messageType { get { return (byte)(union >> 4); } }
    public byte versionPTP { get { return (byte)(union & 0xF); } }
}
joncham
  • 1,584
  • 10
  • 14