Jump to content

PHP: bin2dec

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Here is a simple piece of code I whipped up to do my homework for me. It converts a binary number to its decimal equivalent. However unlike the bindec function, this will preserve the binary/radix point (not to mention it will output the work too).

<?php

/**
 * Convert a binary number with our without
 * a radix point to its decimal equivalent.
 *
 * @param $binary The binary number to convert.
 * @param $output Show the calculations.
 * @return The decimal conversion
 */
function bin2dec($binary, $output = false) {
	$N = 0;
	$o = "";
	list ( $rhs, $lhs ) = explode ( ".", $binary );
	$rhs = strrev ( $rhs );
	for($i = 0; $i < strlen ( $rhs ); $i ++) {
		$d = $rhs [$i] * pow ( 2, $i );
		$N = $d + $N;
		$o = ($d == 0) ? $o : $o . $d . " + ";
	}
	
	for($i = 0; $i < strlen ( $lhs ); $i ++) {
		$d = $lhs [$i] * pow ( 2, - ($i + 1) );
		$N = $d + $N;
		$o = ($d == 0) ? $o : $o . $d . " + ";
	}
	
	return ($output) ? substr ( $o, 0, - 3 ) . " = " . $N : $N;
}

?>

Usage:
echo bin2dec ( "1011101.1000101", true );
Output:
1 + 4 + 8 + 16 + 64 + 0.5 + 0.03125 + 0.0078125 = 93.5390625


#2
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts
If only I had this last semester, my life would have been so much easier. Thanks for writing this because it will help me when I get to writing MIPS code next week :)

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
What would this output?

echo bin2dec ( "1011101.1000101 01011101 10111011", true );  

Why did you not use the PHP Standard Funcion: PHP: bindec - Manual

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
hehe ~ why didn't you use it

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

Jordan said:

What would this output?


echo bin2dec ( "1011101.1000101 01011101 10111011", true );  

That would output an incorrect number. Try it yourself and see :P

Jordan said:

Why did you not use the PHP Standard Funcion: PHP: bindec - Manual

As stated in my post (and I am assuming this), PHP won't consider 1011101.1000101 a valid binary string due to the radix point, plus (again stated in my original post) bindec doesn't show me the "work"

#6
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
My total bad. Any particular reason you wrote it in PHP? Much easier and faster to do this in ASM - not to mention the multitude of examples already out there to do exactly this.

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I know why you are writing it and I know your reasons. You harassed me with those questions when I wrote the Ascii calculator so I figured I'd get you back.

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Lol Jordan.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

TkTech said:

My total bad. Any particular reason you wrote it in PHP? Much easier and faster to do this in ASM - not to mention the multitude of examples already out there to do exactly this.

primarily because I don't know asm.

Posted via CodeCall Mobile

#10
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts

Jordan said:

I know why you are writing it and I know your reasons. You harassed me with those questions when I wrote the Ascii calculator so I figured I'd get you back.

Go Jordan! I should have wrote this a long time ago, but I didn't feel like it and it wouldn't have helped me on the test :P At least John took the time to write it :)

#11
Guest_Jordan_*

Guest_Jordan_*
  • Guests
lol, no motivation like homework.

Posted via CodeCall Mobile