Connecting Tech Pros Worldwide Help | Site Map

protocol header

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 03:57 AM
Andreas Müller
Guest
 
Posts: n/a
Default protocol header

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, 03:57 AM
Uenal Mutlu
Guest
 
Posts: n/a
Default 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, 03:57 AM
Niels Dybdahl
Guest
 
Posts: n/a
Default 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, 03:57 AM
marbac
Guest
 
Posts: n/a
Default 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, 03:57 AM
Julián Albo
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.