note 10685 modified in function.decbin by felipe
| From: | felipe@php.net | Date: | Tue, 22 Jul 2008 03:04:29 +0000 |
| Subject: | note 10685 modified in function.decbin by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142478@lists.php.net to get a copy of this message | ||
Here's a little binary for counting on your fingers up to 1023 (from 0). It formats/pads the
binary number like you wanted.
<?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";
?>
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...
--was--
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>
<?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...
http://php.net/manual/en/function.decbin.php