Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copying chars to ints...

Reply
Thread Tools

copying chars to ints...

 
 
Bob Smith
Guest
Posts: n/a
 
      08-19-2003
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


 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      08-19-2003
"Bob Smith" <(E-Mail Removed)> wrote in message
news:6sn0b.116747$(E-Mail Removed).. .
| 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<< | 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


 
Reply With Quote
 
 
 
 
David Cattarin
Guest
Posts: n/a
 
      08-19-2003
"Bob Smith" <(E-Mail Removed)> wrote in message news:<6sn0b.116747$(E-Mail Removed)> ...
> 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
 
Reply With Quote
 
Frecklefoot
Guest
Posts: n/a
 
      08-19-2003
"Bob Smith" <(E-Mail Removed)> wrote in message news:<6sn0b.116747$(E-Mail Removed)> ...
> 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. :^)
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-19-2003
On 19 Aug 2003 06:22:24 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (David Cattarin)
wrote in comp.lang.c++:

> "Bob Smith" <(E-Mail Removed)> wrote in message news:<6sn0b.116747$(E-Mail Removed)> ...
> > 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
 
Reply With Quote
 
David Cattarin
Guest
Posts: n/a
 
      08-20-2003
Jack Klein <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 19 Aug 2003 06:22:24 -0700, (E-Mail Removed) (David Cattarin)
> wrote in comp.lang.c++:
>
> > "Bob Smith" <(E-Mail Removed)> wrote in message news:<6sn0b.116747$(E-Mail Removed)> ...
> > > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
problem to convert integer to ascii chars for LCD in vhdl Kenneth VHDL 2 08-20-2007 10:29 PM
copying chars in TC++ MC felon C++ 4 12-08-2006 09:33 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM



Advertisments