The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Software Design
|
Written number (two, thirteen) to binary (10, 10101)
Discuss Written number (two, thirteen) to binary (10, 10101) in the Software Design forum on Dev Shed. Written number (two, thirteen) to binary (10, 10101) Software design forum discussing design principles and non-language specific algorithms. Get help with logic, algebraic, or relational concepts.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 1st, 2002, 06:34 AM
|
 |
Web Monkey
|
|
Join Date: May 2001
Location: I refuse to be from anywhere, I am one with the internet
Posts: 76
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
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.
__________________
Quick, think of something geeky to say before they catch on!
Mozilla is your friend!
Web Site
|

October 1st, 2002, 08:04 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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!
[edit] 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.
|

October 1st, 2002, 10:54 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|

October 2nd, 2002, 03:07 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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! 
|

October 14th, 2002, 04:36 PM
|
 |
An Ominous Coward
|
|
|
|
I have honestly never seen a reason to do bitwise operations until this point in my life..... how enlightening! 
|

October 16th, 2002, 09:26 PM
|
|
Junior Member
|
|
Join Date: Jun 2002
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
...
How 'bout this:
PHP Code:
$nowinbinary=sprintf("%b",$wasanint);
You young people always trying to do things the hard way 
|

November 14th, 2002, 11:53 AM
|
 |
Web Developer
|
|
Join Date: Oct 2001
Location: Pennsylvania
Posts: 171
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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 :-)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|