note 103064 deleted from function.base-convert by danbrown
| From: | danbrown@php.net | Date: | Wed, 23 Mar 2011 15:14:28 +0000 |
| Subject: | note 103064 deleted from function.base-convert by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178128@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
some improvement for the functon of fragmer[at]mail[dot]ru 02-May-2005 03:58
the dec2any conversion does not work when the number is 0, since
log10(0) = -INF
one should add a special check for 0.
<?php
// Decimal > Custom
function dec2any( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
,0 ,$base );
}
// check if we have a zero
$t = ($num == 0) ? 0 : floor( log10( $num ) / log10( $base ) );
for ( $t; $t >= 0; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
?>