Connecting Tech Pros Worldwide Forums | Help | Site Map

protocol header

Andreas Müller
Guest
 
Posts: n/a
#1: Jul 23 '05
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?

Uenal Mutlu
Guest
 
Posts: n/a
#2: Jul 23 '05

re: protocol header


"Andreas Müller" wrote[color=blue]
> 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?[/color]

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


Niels Dybdahl
Guest
 
Posts: n/a
#3: Jul 23 '05

re: protocol header


> I'm designing a new simple protocol and now I need a header for it.[color=blue]
> 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?[/color]

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


marbac
Guest
 
Posts: n/a
#4: Jul 23 '05

re: protocol header


Andreas Müller wrote:[color=blue]
> 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?[/color]

Hello,

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

regards marbac

Julián Albo
Guest
 
Posts: n/a
#5: Jul 23 '05

re: protocol header


Andreas Müller wrote:
[color=blue]
> 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?[/color]

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).

--
Salu2
Closed Thread