Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 1st, 2002, 06:34 AM
Zoko Siman's Avatar
Zoko Siman Zoko Siman is offline
Web Monkey
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: I refuse to be from anywhere, I am one with the internet
Posts: 76 Zoko Siman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Send a message via ICQ to Zoko Siman Send a message via AIM to Zoko Siman
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

Reply With Quote
  #2  
Old October 1st, 2002, 08:04 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 34 m 6 sec
Reputation Power: 4080
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.

Reply With Quote
  #3  
Old October 1st, 2002, 10:54 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 34 m 6 sec
Reputation Power: 4080
Just went ahead and wrote a routine to convert english words to decimal, in PHP. I posted that routine in the PHP forum http://forums.devshed.com/showthrea...&threadid=44672

Hope this helps!

Reply With Quote
  #4  
Old October 2nd, 2002, 03:07 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 34 m 6 sec
Reputation Power: 4080
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) ? 0), $mask >>= 1);
print 
"$num is $binary in binary <br>"

Hope this helps!

Reply With Quote
  #5  
Old October 14th, 2002, 04:36 PM
Ctb's Avatar
Ctb Ctb is offline
An Ominous Coward
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2002
Posts: 4,425 Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 10 h
Reputation Power: 0
I have honestly never seen a reason to do bitwise operations until this point in my life..... how enlightening!

Reply With Quote
  #6  
Old October 16th, 2002, 09:26 PM
erydo erydo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 2 erydo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking ...

How 'bout this:

PHP Code:
 $nowinbinary=sprintf("%b",$wasanint); 


You young people always trying to do things the hard way

Reply With Quote
  #7  
Old November 14th, 2002, 11:53 AM
Pasiphae1213's Avatar
Pasiphae1213 Pasiphae1213 is offline
Web Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: Pennsylvania
Posts: 171 Pasiphae1213 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Pasiphae1213 Send a message via Yahoo to Pasiphae1213
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 :-)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Written number (two, thirteen) to binary (10, 10101)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap