copying chars to ints...

B

Bob Smith

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
 
I

Ivan Vecerina

| 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
 
D

David Cattarin

Bob Smith said:
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
 
F

Frecklefoot

Bob Smith said:
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. :^)
 
J

Jack Klein

Bob Smith said:
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
 
D

David Cattarin

Jack Klein said:
Bob Smith said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top