473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

copying chars to ints...

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
Jul 19 '05 #1
5 1908
"Bob Smith" <la******@yahoo.com> wrote in message
news:6s********************@twister.nyroc.rr.com.. .
| 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
Jul 19 '05 #2
"Bob Smith" <la******@yahoo.com> wrote in message news:<6s********************@twister.nyroc.rr.com> ...
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
Jul 19 '05 #3
"Bob Smith" <la******@yahoo.com> wrote in message news:<6s********************@twister.nyroc.rr.com> ...
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. :^)
Jul 19 '05 #4
On 19 Aug 2003 06:22:24 -0700, di****@ix.netcom.com (David Cattarin)
wrote in comp.lang.c++:
"Bob Smith" <la******@yahoo.com> wrote in message news:<6s********************@twister.nyroc.rr.com> ...
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
Jul 19 '05 #5
Jack Klein <ja*******@spamcop.net> wrote in message news:<nu********************************@4ax.com>. ..
On 19 Aug 2003 06:22:24 -0700, di****@ix.netcom.com (David Cattarin)
wrote in comp.lang.c++:
"Bob Smith" <la******@yahoo.com> wrote in message news:<6s********************@twister.nyroc.rr.com> ...
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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
3
by: Rebecca Lovelace | last post by:
For some reason in Enterprise Manager for SQL Server 2000, I cannot put the following line into a trigger: select * into #deleted from deleted When I hit the Apply button I get the following...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
8
by: rngouni | last post by:
hai all, Can any one tell how and where the concept of signed char is implemented.
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
8
by: zefciu | last post by:
Hello! Where can I find a good explanation when does an interpreter copy the value, and when does it create the reference. I thought I understand it, but I have just typed in following...
10
by: webfan | last post by:
In the code below, will the assignment z= y do bitwise copying? struct x { int a; int b; int c; }; main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.