★ wanayoo — archive 1999 http://www.php.net/manual/ro/function.decbin.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | reporting bugs | links 
search for in the  
previouscoshdechexnext
Last updated: Wed, 06 Mar 2002
view this page in Plain HTML | English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Russian | Spanish | Turkish

decbin

(PHP 3, PHP 4 >= 4.0.0)

decbin -- Decimal to binary

Description

string decbin ( int number)

Returns a string containing a binary representation of the given number argument. The largest number that can be converted is 4294967295 in decimal resulting to a string of 32 1's.

See also the bindec() function.

User Contributed Notes
decbin
add a note about notes
daduke@dataway.ch
08-Jan-2001 03:57

Hi,

is there a way to specify the width of the binary word? E. g. I have to
translate 0..7 to "000".."111". Just using decbin
doesn't give "000" but "0".

tnx, Christian

sterba@locksley-west.com
10-Jan-2001 12:55

I had the same problem on PHP 3.0.9, so the str_repeat was not an option.
This maight help:

while (strlen($result)<3) $result="0".$result;

oubiwann@yahoo.com
17-Jan-2001 04:04

To: daduke@dataway.ch

Here's a little binary for counting on your fingers up to 1023 (from 0). It formats/pads the binary number like you wanted. <pre> &lt;?php print "<pre>Dec : Finger Positions\n"; print "---- : ----------------\n"; for ( $i=0; strlen($bin) <=10; ++$i ) { $bin = decbin($i); $output = sprintf("%04d : ", $i); $bin_temp = $bin; while ( strlen($bin_temp) < 10 ) { $bin_temp = "0" . $bin_temp; } print $output . $bin_temp . "\n"; } print "</pre>\n"; ?> </pre> I tried using sprintf("%04d : %10d", $i, $bin) and sprintf("%04d : %10d", $i, $bin + 0), the later an attempt to force the string to act as a number, but neither worked. Maybe it was me...

nospam.me@padamj.com
11-Mar-2001 05:56

do this to pad the binary:

str_pad(decbin($dec),3,"0",STR_PAD_LEFT);

output like:
001
010

sam@barnum.com
12-Apr-2001 07:26

or pad the string like:
   substr("000" . $decbin($i), -3, 3);

ajl@gmx.de
08-Oct-2001 01:47

HERE you can convert 64bit instead of 32bit with the standard decbin
<?
function bigdecbin($dec,$doublewords=1) {
	$erg = "";
	do {
	  	$rest = $dec%2147483648;
	  	if ($rest<0) $rest+=2147483648;
	  	$erg = str_pad(decbin($rest),31,"0",STR_PAD_LEFT).$erg;
	  	$dec = ($dec-$rest)/2147483648;
  	} while (($dec>0)&&(!($dec<1)));
  	
  	return str_pad($erg,$doublewords*31,"0",STR_PAD_LEFT);
}

echo "<pre>";
for ($i=1.5*2147483647.0-10;$i<1.5*2147483647.0+10;$i++) {
	echo "DEC:".$i."
BIN:".bigdecbin($i,2)."
"; } echo "</pre>"; ?>

gthompsn@ucar.edu
22-Jan-2002 11:04

To pad leading zeroes onto the output of decbin, I prefer to use something
like this for my 8-bit integer:
 $qcStr=sprintf("%08d",decbin($myInt));

--Greg T.

php@silisoftware.com
28-Feb-2002 07:15

Another larger-than-31-bit function.
Works for very large numbers, but at the expense of perfect bit-precision
as the size increases (I noticed rounding errors past 16 or so decimal
places) so use with caution, and only when decbin() won't cut it.

function Dec2Bin($number) {
	while ($number >= 256) {
		$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
		$number = floor($number / 256);
	}
	$bytes[] = $number;
	for ($i=0;$i<count($bytes);$i++) {
		$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) :
str_pad(decbin($bytes[$i]), 8, "0", STR_PAD_LEFT)).$binstring;
	}
	return $binstring;
}

add a note about notes
previouscoshdechexnext
Last updated: Wed, 06 Mar 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: Synacor
Last updated: Wed Mar 6 19:17:04 2002 EST