protocol header

  • Thread starter =?ISO-8859-1?Q?Andreas_M=FCller?=
  • Start date
?

=?ISO-8859-1?Q?Andreas_M=FCller?=

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?
 
N

Niels Dybdahl

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?

Most compilers will place the struct you describe in 8 bytes, but some
compilers might not.
And on some processors the most significant byte will be first (Motorola)
and on others the least significant byte will be first (Intel). So you have
to take the byte order into account, f.ex by swapping the bytes on one of
the processor types.

Niels Dybdahl
 
U

Uenal Mutlu

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?

homework?
it's so simple. hint: typecast adress of struct to char*, or use
union and struct and...
 
M

marbac

Andreas said:
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?

Hello,

if you are using C++, then reinterpret_cast might be a solution.

regards marbac
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Andreas said:
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?

Just use a char [8], or better, unsigned char [8] Put/get your data in it in
the format you want to use, and the code will work in all machines (in all
that use the same byte size, at least).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top