Converting binary to decimal  | | |
September 30th, 2003, 04:23 AM
|
#1 (permalink)
| | Junior Member
Join Date: Jul 2003
Posts: 25
| 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. |
| |
September 30th, 2003, 01:33 PM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Utah
Posts: 551
|
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 |
| |
October 1st, 2003, 02:14 AM
|
#3 (permalink)
| | Junior Member
Join Date: Jul 2003
Posts: 25
|
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"? |
| |
October 1st, 2003, 03:25 AM
|
#4 (permalink)
| | Member
Join Date: Apr 2002 Location: Georgia
Posts: 137
|
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Ò®
|
| |
October 5th, 2003, 05:01 AM
|
#5 (permalink)
| | Junior Member
Join Date: Jul 2003
Posts: 25
|
Thanks for the help guys!  |
| |
October 10th, 2005, 07:23 PM
|
#6 (permalink)
| | Junior Member
Join Date: Oct 2005
Posts: 4
|
i just got the same assignment, and am VERY stuck. Does anyone wanna code this up using the cin.get function and reading the values from left to right? Damn hard assignment if u ask me for being in the 5th week of an intro computer science class. |
| |
October 10th, 2005, 09:22 PM
|
#7 (permalink)
| | Member
Join Date: Sep 2002
Posts: 364
|
How do you expect to learn if you ask others to do your assignments? |
| |
October 10th, 2005, 09:26 PM
|
#8 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
Osmosis. After we write it out, he will print it and strap it onto his head with one of those 80's headbands.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
October 11th, 2005, 02:29 AM
|
#9 (permalink)
| | Junior Member
Join Date: Oct 2005
Posts: 4
|
i learn by example. I can't figure out the logic behind it, and if i can't even get passed that step, there is no way to figure out to code it. |
| |
October 11th, 2005, 02:32 AM
|
#10 (permalink)
| | Junior Member
Join Date: Oct 2005
Posts: 4
|
my prof says to convert it "on the fly", and i don't even know what she means by that |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |