I have code that reads a stream of bytes from a serial port using a propriety protocol. In the end I have a array of bytes that represents a carefully aligned structure sent by the other end of the connection. Part of that struct is a an array of other structs. C# does not seem to allow fixed arrays with anything other than base types. In C it is simple. I can craft a struct definition including the fixed array of structs and just cast a pointer of the type to the beginning of the byte array. I can then dereference the pointer to access individual members. Simple. What is the best way of doing this in C#?
e.g. In C:
byte serialBuff[40]; // holds an array of bytes containing the message I want;
typedef struct { char x; char y } point_t;
typedef struct { char trId; char ack, point_t points[3]; } message_t;
// message_t has the following representation in my byte buffer:
// point[0], point[1], point[2]
// each element is a byte
// --------------------------------------------
// | trId | ack | x | y | x | y | x | y |
// --------------------------------------------
// ^
// serialBuff
To access this message correctly all I need do is:
message_t *myMessage = (message_t *)serialBuff;
Is there any way to do this in C#, or do I have to read in each byte and copy it into individual struct or class members? So far I have written code to read in the first part of the message then another to read in the array of points and copy them to a struct. Very ugly and inefficient I think - but it works!
I guess the unsafe keyword could work here:
(Thrown together quickly so you get the idea)