copying chars to ints...

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

1. Bob SmithGuest

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

2. Ivan VecerinaGuest

"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

3. David CattarinGuest

"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
4. FrecklefootGuest

"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
5. Jack KleinGuest

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
6. David CattarinGuest

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

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.