Solved

Convert to Binary

Posted on 2004-09-24
2
321 Views
Last Modified: 2012-05-05
Does anyone know an algorithm to convert a decimal STRING, (not a 'number') to it's binary equivalent?

i.e.: "4654879791" -> "100010101011100111101010000101111"
0
Comment
Question by:eternal_21
2 Comments
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 250 total points
ID: 12146326
Cast the string to a number.  Proceed as normal.
0
 
LVL 22

Accepted Solution

by:
grg99 earned 250 total points
ID: 12146484
You can't convert decimal to binary sequentially, a digit at a time, as each decimal digit expands across several binary digits.

the only way I can see is to add up the powers of ten til you get the whole number, then convert to binary:

Tot = 0;

for all digits:  Tot = Tot * 10 + this_digit;

while Tot isnt Zero:  if Tot is odd, print "1" else print "0";  divide Tot by two;

{ Then flip the paper over so you see the digits in reverse order!  }




0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Proportion 4 49
Math home work questions 5 97
Geomentry-Fundamental concepts 6 66
110V Lasko bladeless fan blows with a burning smell 5 77
A Guide to the PMT, FV, IPMT and PPMT Functions In MS Excel we have the PMT, FV, IPMT and PPMT functions, which do a fantastic job for interest rate calculations.  But what if you don't have Excel ? This article is for programmers looking to re…
Introduction On a scale of 1 to 10, how would you rate our Product? Many of us have answered that question time and time again. But only a few of us have had the pleasure of receiving a stack of the filled out surveys and being asked to do somethi…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question