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.

 Dev Shed Forums Sponsor:
#1
October 1st, 2002, 06:34 AM
 Zoko Siman
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

#2
October 1st, 2002, 08:04 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382
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!

 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
October 1st, 2002, 10:54 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382
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!

#4
October 2nd, 2002, 03:07 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382
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) ? 1 : 0), \$mask >>= 1); print "\$num is \$binary in binary <br>";  ```

Hope this helps!

#5
October 14th, 2002, 04:36 PM
 Ctb
An Ominous Coward

Join Date: Jan 2002
Posts: 4,425
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!

#6
October 16th, 2002, 09:26 PM
 erydo
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

#7
November 14th, 2002, 11:53 AM
 Pasiphae1213
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 :-)

 Viewing: Dev Shed Forums > Programming Languages - More > Software 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 Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

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