### Thread: Written number (two, thirteen) to binary (10, 10101)

1. #### Written number (two, thirteen) to binary (10, 10101)

Does anyone have any ideas about how this might be accomplished? In PHP an alogorithm that converts textual numeric strings to binary? And not a million (if {..} else{..}) statments.

Just a thought.
2. There are plenty of ways to do this. Most algorithms usually incrementally divide the number by 2 and set the bits to 1 or 0 depending on the reminder. However, there are other ways of doing it. Here's one quick and dirty way:

1. First convert the number to hex (in PHP, the function is dechex()) and store the result in hexstring
2. Go thru each character of the hexstring and convert it to equivalent binary number according to the table below
Code:
```0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
...
...
D = 1101
E = 1110
F = 1111```
3. Append the binary equivalent to the resultant string

So for a decimal number such as 61518, it would go something like this:
decimal 61518 = F04E in hex
Then to convert F04E into binary we go through each digit in turn:
Equivalent for F = 1111
0 = 0000
4 = 0100
E = 1110

Therefore 61518 (decimal) = F04E (hex) = 1111 0000 0100 1110 in binary.

Hope this helps!

 DUH! Just noticed you needed to convert a written number. Guess you should search on how to convert a written number to a decimal value first and then go from there [/edit]
Last edited by Scorpions4ever; October 1st, 2002 at 08:09 PM.
3. Just went ahead and wrote a routine to convert english words to decimal, in PHP. I posted that routine in the PHP forum Converting english words to number

Hope this helps!
4. Another nifty way to do this is to use bitwise operators. This way you can avoid a lot of steps. Here's how to do it:

1. Initialize a bit mask with the most significant bit set ( ie. 0x80000000 in hex or 1000 000 0000 0000 0000 000 0000 0000 in binary for a 32 bit number)

2. Perform a bitwise and of the mask with the number. If the result is non-zero, append 1 to your string, else append 0. Basically your mask tests if the bit for a particular position is on or not.

3. Shift the mask right by 1 bit

4. Repeat steps 2 and 3 until the mask becomes 0. The resultant string is the value in binary

Coding this in php would go something like this:
PHP Code:
``` <? \$num = 123456; \$mask = 0x8000000; \$binary = ""; \$count = 0; while (\$mask){     if ((\$num & \$mask) == \$mask)         \$binary .= 1;     else         \$binary .= 0;     \$mask >>= 1; } print "\$num is \$binary in binary <br>"; ?> ```
If you want to impress your friends, confound your enemies or just show off by writing the whole thing in a single line, you can also reduce the above algorithm into something like this:
PHP Code:
``` \$num = 123456; for (\$mask=0x8000000, \$binary=""; \$mask; \$binary .= ((\$num & \$mask) ? 1 : 0), \$mask >>= 1); print "\$num is \$binary in binary <br>";  ```
Hope this helps!
5. I have honestly never seen a reason to do bitwise operations until this point in my life..... how enlightening!
6. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2002
Posts
2
Rep Power
0

#### ...

How 'bout this:

PHP Code:
``` \$nowinbinary=sprintf("%b",\$wasanint);  ```
You young people always trying to do things the hard way
7. I believe the purpose of the question was to find the algorithmic way to converting decimal to binary, not language specific, like your PHP example.

That's why us young people try to do things the hard way :-)