Andreas Müller wrote:
> hi @all,
>
> I'm designing a new simple protocol and now I need a header for it.
> There should only be 3 fields, one 32bit and 2 16bit fields -> 64bit
> header (8byte). Due to the used library I need the data as a
(unsigned
> char*) for delivering. I thought about a struct to store the header
but
> I don't know how to cast it to the expected format for delivering.
> Is there a way to store all this information and deliver it in a
char[8]
> which would be exactly 8byte long?
#pragma pack(...) // see your compiler documentation for alignment
struct protocol {
int one;
short two;
short three;
};
protocol p;
// ...
char buf[8] = "";
memcpy (buf, (void*) &p, sizeof (buf));
Instead of int and short you better use the appropriate typedefs for
your platform.
::A::
|