Solved

Convert to Binary

Posted on 2004-09-24
2
322 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Relative Frequency 5 60
Rounding Values 11 60
Need a nodal sequencing tool 3 111
How to get the score in percent of an activity? 7 65
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
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…

821 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