question

M

Martin Ambuhl

Darklight said:
how do one convert decimal to binary?

int main(void)
{
int x = 37;
x = x; /* x is now 37 (base 10) */
x = x; /* x is now 25 (base 16) */
x = x; /* x is now 45 (base 8) */
x = x; /* x is now 100101 (base 2) */
x = x; /* x is now 1b (base 26) */
return 0;
}

Easy, isn't it?
 
M

Markus Moll

Hi
how do one convert decimal to binary?

One rewrites a sum of (distinct) powers of 10 as a sum of (distinct) powers
of 2, i.e.:

\sum_{i=0}^n a_i 10^i = \sum_{i=0}^m b_i 2^i
where 0 <= a_i < 10, 0 <= b_i < 2 for all i

However, this has nothing to do with the C programming language.

I'm sorry, I really don't know what you mean by "convert". Do you want
convert a decimal string representation of a number to its corresponding
binary representation? Please explain what exactly you want to do...

BTW: "question" is one of the worst subject lines you could think of.

Markus
 
R

Richard Heathfield

Darklight said:
how do one convert decimal to binary?

This is really a comp.programming question. Still, since you're here...

The words "decimal" and "binary" describe, not numbers, but
*representations* of numbers.

Let us assume we have a string, D, containing a decimal representation of a
number. For simplicity's sake, we will assume the number is non-negative.

The easiest way to do this is to convert D into a number N, which we can do
using, say, strtoul().

To store the binary representation in a string, we'll need some storage B -
conservatively, 4 * L + 1 bytes, where L is the length of D. We will need
to check that this storage is available before proceeding. For simplicity's
sake, we'll initialise every byte in B to 0, except that we'll set B[0] to
'0' rather than 0.

Pseudocode:

pos = 0
While N > 0
B[pos++] = N mod 2 + '0'
N /= 2
Wend
REM We now have the binary representation, but it's back to front
REM so we need to reverse it
If pos > 0
x = 0
y = pos - 1
While x > y
t = B[x]
B[x] = B[y]
B[y] = t
Wend
Endif

Job done. Your mission, should you choose to accept it, is to find how to
convert the above into a C function.
 
S

SM Ryan

# how do one convert decimal to binary?

scanf and related functions (fscanf, sscanf, etc).
Or functions like strtol and strtod.
 
K

Keith Thompson

[ '#' quoting character corrected ]
SM Ryan said:
scanf and related functions (fscanf, sscanf, etc).
Or functions like strtol and strtod.

The *scanf functions don't support binary. They do support octal and
hexadecimal, which can easily be converted to and from binary.
 
M

Malcolm

Darklight said:
how do one convert decimal to binary?
"binary" can mean two things, either the machine representation (bits set or
unset) or a string of ASCII 1s and 0s, which the user can read as binary.

It is possible but unnecessarily wieldy to convert directly from an ASCII
decimal to and ASCII binary format. Normally we convert everything to and
from machine representation.

The functions sscanf(), atoi() or strtol() will all convert a number from
decimal to machine representation for you.
To convert from machine repreentation to ASCII binary is easy. Just AND with
0x01 to get the first (lowest) bit, 0x02 to get the second bit, 0x40 to get
the third bit.

If your are asking, "how does a function like strtol() work", the answer is
that you take the first digit, and use a look-up table to get the value in
machine representation (0-9). Then you check for a second digit. If the
second digit exists, mutliply by ten, convert the second digit, and add.
repeat until you run out of digits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top