Structures to byte arrays and vice versa?

  • Thread starter Thread starter ORC
  • Start date Start date
O

ORC

Hi,
I have posted almost the same message in the compact framework group, but it
seems to got lost in a deep message structure :-) so I'll try again:

I'll receive a byte array from a serial port. The byte arrays are actually
serialized structures with alignments of 4 (pack = 4). The structures were
originally written in C++ and looks e.g. like this:
struct Parameters
{
double CreatedDate;
byte Initials[13];
double TempSensorOffset;
double TempSensorSlope;
double MathReverseCoef[3][3];
double MathForwardCoef[3][3];
double StrainsOffset[3];
double StrainsSlope[3];
};

In C++ I just had to create a Union consisting of the structure and a byte
array and then I had directly access to both withhout having to do any kind
of conversion. How should I do this in C# using (I need to both send and
receive so the I'm looking for a code that both serializes and de-serializes
the structure)? The code must be supported by the Compact Framework.

- If your advise is to use the CustomMarshaler from opennetcf then:
Does the Marshaller work in both full and compact framework? - does anyone
have a simple example on how to use it (I have read the "Creating a Custom
Marshaler for the .NET Compact Framework" article , but I'm afraid it is too
heavy for me at the state I am - please clarify.

Thank you very much for your help!
Ole
 
A safe and simple approach would be to use the (strangely named)
System.BitConverter class, which converts between primitive types and byte
arrays.
You'll still have to do some work to return a single byte array for your
class/structure.
 
Back
Top