copying chars to ints...

Discussion in 'C++' started by Bob Smith, Aug 19, 2003.

  1. Bob Smith

    Bob Smith Guest

    Hello all, I have a question and I am not sure how I should go about doing
    it.
    I have two unsigned chars and one int. I want to copy both chars to the
    int.
    for example if char a was represented binary by 10000110 and char 2 by
    10101010
    I would like my int to be represented by 1000011010101010 I know short in
    would probably be
    best but for right now int will work, I may have additions I might need to
    add.
    Just asssinging the value from char to int will set the LSB correctly. How
    can i get the MSB in the
    right place?
    I am using G++ on a redhat 7.3 system
    thanks
     
    Bob Smith, Aug 19, 2003
    #1
    1. Advertisements

  2. "Bob Smith" <> wrote in message
    news:6sn0b.116747$...
    | Hello all, I have a question and I am not sure how I should go about doing
    | it.
    | I have two unsigned chars and one int. I want to copy both chars to the
    | int.
    | for example if char a was represented binary by 10000110 and char 2 by
    | 10101010
    | I would like my int to be represented by 1000011010101010 I know short in
    | would probably be
    | best but for right now int will work, I may have additions I might need to
    | add.
    | Just asssinging the value from char to int will set the LSB correctly.
    How
    | can i get the MSB in the
    | right place?

    Assuming that the chars store an 8-bit value, here's a way to do it:

    unsigned int combine(unsigned char MSB, unsigned char LSB)
    {
    return (MSB<<8) | LSB;
    }

    Be careful to use "unsigned char" instead of "char", as "char" is a
    signed type on some platforms. Using the bit-shift operator (<<) on
    a signed value may not do what you expect...


    hth,
    Ivan

    --
    http://www.post1.com/~ivec
     
    Ivan Vecerina, Aug 19, 2003
    #2
    1. Advertisements

  3. "Bob Smith" <> wrote in message news:<6sn0b.116747$>...
    > Hello all, I have a question and I am not sure how I should go about doing
    > it.
    > I have two unsigned chars and one int. I want to copy both chars to the
    > int.
    > for example if char a was represented binary by 10000110 and char 2 by
    > 10101010
    > I would like my int to be represented by 1000011010101010 I know short in
    > would probably be
    > best but for right now int will work, I may have additions I might need to
    > add.
    > Just asssinging the value from char to int will set the LSB correctly. How
    > can i get the MSB in the
    > right place?
    > I am using G++ on a redhat 7.3 system
    > thanks


    If you are doing this a lot, then the best way is to write a helper
    class, or to define a union, something like this:
    union MyUnion
    {
    char c[ 2 ];
    int i;
    };

    Depending on the endianness of your system, you'll need to c[ 0 ] or
    c[ 1 ] will be the MSB.

    Dave
     
    David Cattarin, Aug 19, 2003
    #3
  4. Bob Smith

    Frecklefoot Guest

    "Bob Smith" <> wrote in message news:<6sn0b.116747$>...
    > Hello all, I have a question and I am not sure how I should go about doing
    > it. I have two unsigned chars and one int. I want to copy both chars to the
    > int. for example if char a was represented binary by 10000110 and char 2 by
    > 10101010 I would like my int to be represented by 1000011010101010 I know
    > short in would probably be best but for right now int will work, I may have
    > additions I might need to add.
    > Just asssinging the value from char to int will set the LSB correctly. How
    > can i get the MSB in the right place?


    You can do it by using the shift operators and binary ORs. This is how you'd do it:

    char chA = 86; // binary 10000110
    char chB = 170; // binary 10101010
    int nC = 0; // aggregated int

    nC = chA << 8; // shift left by 8 bits (1 byte)
    nC |= chB; // OR in the remaining bits

    Hope this helped. :^)
     
    Frecklefoot, Aug 19, 2003
    #4
  5. Bob Smith

    Jack Klein Guest

    On 19 Aug 2003 06:22:24 -0700, (David Cattarin)
    wrote in comp.lang.c++:

    > "Bob Smith" <> wrote in message news:<6sn0b.116747$>...
    > > Hello all, I have a question and I am not sure how I should go about doing
    > > it.
    > > I have two unsigned chars and one int. I want to copy both chars to the
    > > int.
    > > for example if char a was represented binary by 10000110 and char 2 by
    > > 10101010
    > > I would like my int to be represented by 1000011010101010 I know short in
    > > would probably be
    > > best but for right now int will work, I may have additions I might need to
    > > add.
    > > Just asssinging the value from char to int will set the LSB correctly. How
    > > can i get the MSB in the
    > > right place?
    > > I am using G++ on a redhat 7.3 system
    > > thanks

    >
    > If you are doing this a lot, then the best way is to write a helper
    > class, or to define a union, something like this:
    > union MyUnion
    > {
    > char c[ 2 ];
    > int i;
    > };
    >
    > Depending on the endianness of your system, you'll need to c[ 0 ] or
    > c[ 1 ] will be the MSB.
    >
    > Dave


    Of course this works in practice on most implementations, but it is
    undefined behavior.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
     
    Jack Klein, Aug 20, 2003
    #5
  6. Jack Klein <> wrote in message news:<>...
    > On 19 Aug 2003 06:22:24 -0700, (David Cattarin)
    > wrote in comp.lang.c++:
    >
    > > "Bob Smith" <> wrote in message news:<6sn0b.116747$>...
    > > > Hello all, I have a question and I am not sure how I should go about doing
    > > > it.
    > > > I have two unsigned chars and one int. I want to copy both chars to the
    > > > int.
    > > > for example if char a was represented binary by 10000110 and char 2 by
    > > > 10101010
    > > > I would like my int to be represented by 1000011010101010 I know short in
    > > > would probably be
    > > > best but for right now int will work, I may have additions I might need to
    > > > add.
    > > > Just asssinging the value from char to int will set the LSB correctly. How
    > > > can i get the MSB in the
    > > > right place?
    > > > I am using G++ on a redhat 7.3 system
    > > > thanks

    > >
    > > If you are doing this a lot, then the best way is to write a helper
    > > class, or to define a union, something like this:
    > > union MyUnion
    > > {
    > > char c[ 2 ];
    > > int i;
    > > };
    > >
    > > Depending on the endianness of your system, you'll need to c[ 0 ] or
    > > c[ 1 ] will be the MSB.
    > >
    > > Dave

    >
    > Of course this works in practice on most implementations, but it is
    > undefined behavior.


    Actually, what I posted was just wrong, it really should be this:

    union MyUnion
    {
    char c[ sizeof( int ) ]; // On most systems int is 4 bytes
    int;
    };

    Anyway, aside from the fact that standard does not specify endianness,
    nor the size of an int, are there any other areas where the code is
    undefined? I don't belive alignment is an issue in this case, but I
    could be wrong there. As long as there was a test for endianness,
    compile-time or otherwise, and c[ ] was filled correctly, this should
    be fairly portable, though I'd be wary of trying it on an embedded
    system. Are there any systems where this would be a problem?

    In general, I've found that most bit-twiddling code ventures into the
    undefined territory and I don't think there is any way around that.
    Dave
     
    David Cattarin, Aug 20, 2003
    #6
    1. Advertisements

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. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    22,240
    Dale King
    Jul 9, 2003
  2. Timo Nentwig
    Replies:
    6
    Views:
    730
  3. Replies:
    3
    Views:
    814
    Mark P
    Apr 3, 2005
  4. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    1,159
    Jack Klein
    Jul 10, 2004
  5. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,788
    Tim Rentsch
    Sep 23, 2005
  6. MC felon

    copying chars in TC++

    MC felon, Dec 7, 2006, in forum: C++
    Replies:
    4
    Views:
    478
    Aston Martin
    Dec 8, 2006
  7. Hongyu
    Replies:
    9
    Views:
    1,212
    James Kanze
    Aug 8, 2008
  8. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    486
    Dan Rogers
    Nov 16, 2004
Loading...