»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 09-30-2003, 01:23 AM   #1 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 11
Toasted Onions is on a distinguished road
Converting binary to decimal

I need to convert binary (base-2) numbers to decimal (base-10) numbers using C++. And I have to do it one digit at a time, left to right, using the cin.get function.

I'm having trouble with this.

Any help?

Thanks.

Toasted Onions is offline   Reply With Quote
Old 09-30-2003, 10:33 AM   #2 (permalink)
Registered User
 
Join Date: Oct 2001
Location: Utah
Posts: 543
dragonb is on a distinguished road
from left to right?
it'd be easier the other way,
But just start doing some pseudo code for your algorithm.

I suggest reading all the values into an array, Because you have to know how many.
Then just go along the array(backwards starting at lowest digit) and multiply the number by 2 to the x where x is the position.

So...

read in numbers, init array..

for loop i from 1 to length of array
total = total + array[i] * 2^i
end loop

you'll need to play with the loop, make sure it's going the right length, multiplying the right stuff, etc..

good luck..
dragonb
dragonb is offline   Reply With Quote
Old 09-30-2003, 11:14 PM   #3 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 11
Toasted Onions is on a distinguished road
Sorry, I'm a total novice with this stuff.

I don't think I know how to create an array.

I also need to give an "invalid" message when a non-binary number is entered. What command do I use to make sure it's binary?

I mean:

cout << "Enter binary number: ";

while (number != binaryBase2)
cout << "Invalid, try again";

What can I put in place of "binaryBase2" to make it work? And if the user has to try again, how do I make it jump back up to the "enter binary number"?
Toasted Onions is offline   Reply With Quote
Old 10-01-2003, 12:25 AM   #4 (permalink)
Registered User
 
Join Date: Apr 2002
Location: Georgia
Posts: 137
Jüš† ä gü¥ is on a distinguished road
Well, why don't you first off set a limit as to maximum number of bits that the largest number that can be read in can be.

Then, you find out how many bits are in the number.

Then, if there is a 1 in that bit position, add in 2 raised to the power of that bit position to your running total.

Do this from left to right, stripping off each bit as you do so.

The answer should be the decimal equivalent of that binary number. (Check it by using the Calculator on your computer to see if you did this right)

Also, a binary number only has 0's and 1's. Any number with digits other than 0's and 1's isn't binary. That should be enough information for you to test if it is binary or not.
__________________
Jüš† ä €öm¶ù†Ê® §ÇÌÈñŒ mÅjÒ®
Jüš† ä gü¥ is offline   Reply With Quote
Old 10-05-2003, 02:01 AM   #5 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 11
Toasted Onions is on a distinguished road
Thanks for the help guys!
Toasted Onions is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 10:10 PM.