Convertion from Binary to Decimal

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FlashPack
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 5:37 pm
Location: Egypt

Convertion from Binary to Decimal

Post by FlashPack »

hi everyone
that's my first post here .. hope u like it
i've made a function to convert from binary to decimal system
some people might say there is already a built-in one called decbin() ....
but that function doesn't convert fractions ... thats why i made it :
here it is :

Code: Select all

 
function BinaryToDecimal($binary){
$binary=trim($binary);
if (strstr($binary,'.')){
$split=explode('.',$binary);
$integer=$split[0];
$fraction=$split[1];
 
 
$digits=str_split($fraction);
$num=sizeof($digits);
for ($i=1; $i<=$num;$i++){
if ($digits[$i-1]>1){
echo '<script>alert("Enter Binary Digits Only {0,1}\n \n eg: 11001 or 11001.011");history.go(-1)</script> ';
 
}
$exponent=pow(2,-$i);
$fraction_result+=$digits[$i-1]*$exponent;
}
 
}else{
$integer=$binary;
}
 
$splits=str_split($integer);
$num=sizeof($splits)-1;
$i=$num;
foreach($splits as $digits){
if ($digits>1){
echo '<script>alert("Enter Binary Digits Only {0,1}\n \n eg: 11001 or 11001.011");history.go(-1)</script> ';
}
$exponent=pow(2,$i);
$integer_result+=$digits*$exponent;
$i--;
}
if($fraction_result){
$result=$integer_result+$fraction_result;
}else {
$result=$integer_result;
}
return $result ;
}
 
i'm ready for any question

bye now
Last edited by FlashPack on Tue Nov 11, 2008 2:36 pm, edited 2 times in total.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Convertion from Binary to Decimal

Post by mmj »

If I understand you correctly why don't you you just split the string at the decimal point then convert each one separately?

(note: this comment could easily contain incorrect info, I haven't ventured into this field too much)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Convertion from Binary to Decimal

Post by VladSun »

FlashPack wrote: called decbin() ....
but that function doesn't convert fractions ...
That's because a floating point format is used. The fixed point format is rarely used, but still it's not the case (like 11001.011) you are trying to solve.

http://en.wikipedia.org/wiki/Floating_point
http://en.wikipedia.org/wiki/Fixed_point
There are 10 types of people in this world, those who understand binary and those who don't
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Convertion from Binary to Decimal

Post by Mark Baker »

FlashPack wrote:hi everyone
that's my first post here .. hope u like it
i've made a function to convert from binary to decimal system
some people might say there is already a built-in one called decbin() ....
but that function doesn't convert fractions ... thats why i made it :
Had you looked at pack() and unpack()?
FlashPack
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 5:37 pm
Location: Egypt

Re: Convertion from Binary to Decimal

Post by FlashPack »

mmj wrote:If I understand you correctly why don't you you just split the string at the decimal point then convert each one separately?
that's exactly what the function do ...
VladSun wrote:That's because a floating point format is used. The fixed point format is rarely used, but still it's not the case (like 11001.011) you are trying to solve.
thank you for ur comment
Had you looked at pack() and unpack()?
pack — Pack data into binary string >> http://www.php.net/manual/en/function.pack.php
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Convertion from Binary to Decimal

Post by mmj »

If that is the case then wouldn't this work?

Code: Select all

$bin = '1101.11';
$parts = explode('.', $bin);
$dec = bindec($parts[0]) + (bindec($parts[1]) / 10);
(error checking not included)
FlashPack
Forum Newbie
Posts: 5
Joined: Mon Nov 10, 2008 5:37 pm
Location: Egypt

Re: Convertion from Binary to Decimal

Post by FlashPack »

mmj wrote:If that is the case then wouldn't this work?

Code: Select all

$bin = '1101.11';
$parts = explode('.', $bin);
$dec = bindec($parts[0]) + (bindec($parts[1]) / 10);
(error checking not included)
it works :idea: but it doesn't return the accurate value :!:
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Convertion from Binary to Decimal

Post by mmj »

FlashPack wrote:
mmj wrote:If that is the case then wouldn't this work?

Code: Select all

$bin = '1101.11';
$parts = explode('.', $bin);
$dec = bindec($parts[0]) + (bindec($parts[1]) / 10);
(error checking not included)
it works :idea: but it doesn't return the accurate value :!:
For that example I'm pretty sure it worked correctly.

You have to check how many zeros are after the decimal and divide it by that number:

Code: Select all

$bin = '1101.011';
$parts = explode('.', $bin);
$zeros = substr_count($parts[1], '0') + 1;
$dec = bindec($parts[0]) + (bindec($parts[1]) / pow(10, $zeros));
I could easily be incorrect though. :)

EDIT:

that won't work. corrected:

Code: Select all

$bin = '1101.11';
$parts = explode('.', $bin);
for ($i = 0; $parts[1][$i] === '0'; $i++); //php does type conversion on '0' in some cases
$dec = bindec($parts[0]) + (bindec($parts[1]) / pow(10, $i + 1));
Last edited by mmj on Wed Nov 12, 2008 4:49 pm, edited 1 time in total.
Post Reply