protocol header

Discussion in 'C++' started by =?ISO-8859-1?Q?Andreas_M=FCller?=, May 4, 2005.

  1. 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?
     
    =?ISO-8859-1?Q?Andreas_M=FCller?=, May 4, 2005
    #1
    1. Advertising

  2. > 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
     
    Niels Dybdahl, May 4, 2005
    #2
    1. Advertising

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

    Uenal Mutlu Guest

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


    homework?
    it's so simple. hint: typecast adress of struct to char*, or use
    union and struct and...
     
    Uenal Mutlu, May 4, 2005
    #3
  4. =?ISO-8859-1?Q?Andreas_M=FCller?=

    marbac Guest

    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?


    Hello,

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

    regards marbac
     
    marbac, May 4, 2005
    #4
  5. Andreas Müller wrote:

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

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, May 4, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    729
    lilburne
    Nov 2, 2003
  2. =?ISO-8859-1?Q?Andreas_M=FCller?=

    protocol header

    =?ISO-8859-1?Q?Andreas_M=FCller?=, May 4, 2005, in forum: C++
    Replies:
    1
    Views:
    341
    Abecedarian
    May 4, 2005
  3. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    624
    Jack Klein
    Jul 24, 2004
  4. Replies:
    3
    Views:
    1,331
    Walter Roberson
    May 1, 2006
  5. mlt
    Replies:
    2
    Views:
    881
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page