Connecting Tech Pros Worldwide Help | Site Map

protocol header

  #1  
Old July 23rd, 2005, 04:57 AM
Andreas Müller
Guest
 
Posts: n/a
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?
  #2  
Old July 23rd, 2005, 04:57 AM
Uenal Mutlu
Guest
 
Posts: n/a

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


  #3  
Old July 23rd, 2005, 04:57 AM
Niels Dybdahl
Guest
 
Posts: n/a

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


  #4  
Old July 23rd, 2005, 04:57 AM
marbac
Guest
 
Posts: n/a

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

  #5  
Old July 23rd, 2005, 04:57 AM
Julián Albo
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some help on header refresh... Justin answers 19 March 10th, 2007 02:05 PM
Why do I get "The server committed a protocol violation"? Scott McDermott answers 3 July 19th, 2006 01:05 PM
if-modified-since question (protocol problem?) hug answers 102 May 14th, 2006 09:15 PM
send tcp raw socket (bogus tcp header length) Tiger answers 5 May 1st, 2006 07:05 AM