extracting integer from array of char

Discussion in 'C Programming' started by Mantorok Redgormor, Oct 19, 2003.

  1. If I have an array of char and there is an integer located in that
    char that was assigned by:
    int foo=10;
    *(int *)&array[position] = foo;

    How can I extract that integer? Any portable way?
    I can assign using the above method but can't retrieve it with the
    above method.

    Also, what is a portable method of sending and receiving a struct that
    contains bitfields across a network to two different machines? one is
    linux on x86 the other is solaris-sparc.. any idea? Very frustrating
    problem.
     
    Mantorok Redgormor, Oct 19, 2003
    #1
    1. Advertising

  2. Mantorok Redgormor

    CBFalconer Guest

    Mantorok Redgormor wrote:
    >
    > If I have an array of char and there is an integer located in
    > that char that was assigned by:
    > int foo=10;
    > *(int *)&array[position] = foo;


    Bad. Casts are evil. Very likely to cause UB due to alignment
    restrictions. Assuming array is an array of char, all you need
    is:

    array[position] = foo;

    It's up to you to ensure that foo is in the range a char can
    hold. You find out by testing against the values in limits.h.

    >
    > How can I extract that integer? Any portable way?
    > I can assign using the above method but can't retrieve it with
    > the above method.


    by:
    foo = array[position];
    >
    > Also, what is a portable method of sending and receiving a
    > struct that contains bitfields across a network to two different
    > machines? one is linux on x86 the other is solaris-sparc.. any
    > idea? Very frustrating problem.


    Extract each field, convert it to a text representation, and send
    that. The binary image on one machine need not have any
    particular relationship to the binary image on another machine.

    The recommended method even allows you to follow the process
    during operation :)

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 19, 2003
    #2
    1. Advertising

  3. Mantorok Redgormor

    Malcolm Guest

    "Mantorok Redgormor" <> wrote in message
    > If I have an array of char and there is an integer located in that
    > char that was assigned by:
    > int foo=10;
    > *(int *)&array[position] = foo;
    >
    > How can I extract that integer? Any portable way?
    >

    This is pretty horrible. If the same program does the assignment and
    extraction, you can use memcpy()
    memcpy(&foo, &array[position], sizeof(int)).
    >
    > I can assign using the above method but can't retrieve it with the
    > above method.
    >

    That's surprising. If the compiler allows a cast one way it should allow it
    in the reverse direction.
    >
    > Also, what is a portable method of sending and receiving a struct that
    > contains bitfields across a network to two different machines? one is
    > linux on x86 the other is solaris-sparc.. any idea? Very frustrating
    > problem.
    >

    You are not guaranteed the same alignment for any structs (except within the
    same program, of course).
    You need to break the structure down into its elements, and send it one byte
    at a time (all integers 32 bits big-endian, all chars ascii, all floats in
    32-bit IEEE format, etc).
     
    Malcolm, Oct 19, 2003
    #3
    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. lovecreatesbeauty
    Replies:
    1
    Views:
    1,159
    Ian Collins
    May 9, 2006
  2. Replies:
    3
    Views:
    763
  3. davidb
    Replies:
    0
    Views:
    811
    davidb
    Sep 1, 2006
  4. davidb
    Replies:
    6
    Views:
    1,599
    Default User
    Sep 1, 2006
  5. Replies:
    5
    Views:
    248
    Tassilo v. Parseval
    Nov 22, 2005
Loading...

Share This Page