home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Converting binary to decimal

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1577
Discussions: 200,955, Posts: 2,379,493, Members: 246,327
Old September 30th, 2003, 04:23 AM   Digg it!   #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.
Toasted Onions is offline   Reply With Quote
Old 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
dragonb is offline   Reply With Quote
Old 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"?
Toasted Onions is offline   Reply With Quote
Old 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Ò®
Jüš† ä gü¥ is offline   Reply With Quote
Old October 5th, 2003, 05:01 AM     #5 (permalink)
Junior Member
 
Join Date: Jul 2003
Posts: 25
Thanks for the help guys!
Toasted Onions is offline   Reply With Quote
Old 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.
ryan9939 is offline   Reply With Quote
Old 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?
Creosote is offline   Reply With Quote
Old October 10th, 2005, 09:26 PM     #8 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old 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.
ryan9939 is offline   Reply With Quote
Old 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
ryan9939 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2947)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
Is the PSU I received dead? (12)
Print spooler problem (15)
windows vista security holes (11)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Recent Discussions
Open With ..... Win7 (1)
Kingston Bluetooth Dongle Driver (0)
webcam (0)
upgrade for hp a6101 (0)
windows vista security holes (11)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)


All times are GMT -4. The time now is 06:58 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28