Page 1 of 1

Convertion from Binary to Decimal

Posted: Tue Nov 11, 2008 5:46 am
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

Re: Convertion from Binary to Decimal

Posted: Tue Nov 11, 2008 9:35 am
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)

Re: Convertion from Binary to Decimal

Posted: Tue Nov 11, 2008 12:09 pm
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

Re: Convertion from Binary to Decimal

Posted: Tue Nov 11, 2008 12:30 pm
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()?

Re: Convertion from Binary to Decimal

Posted: Tue Nov 11, 2008 2:45 pm
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

Re: Convertion from Binary to Decimal

Posted: Wed Nov 12, 2008 2:21 am
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)

Re: Convertion from Binary to Decimal

Posted: Wed Nov 12, 2008 2:16 pm
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 :!:

Re: Convertion from Binary to Decimal

Posted: Wed Nov 12, 2008 3:00 pm
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));