note 19498 added to function.decbin
| From: | php at silisoftware dot com | Date: | Fri, 01 Mar 2002 00:15:16 +0000 |
| Subject: | note 19498 added to function.decbin | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-27209@lists.php.net to get a copy of this message | ||
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;
}
--
http://www.php.net/manual/en/function.decbin.php
http://master.php.net/manage/user-notes.php?action=edit+19498
http://master.php.net/manage/user-notes.php?action=delete+19498
http://master.php.net/manage/user-notes.php?action=reject+19498