Created
July 2, 2009 05:53
-
-
Save wmh/139286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Decimal > Custom | |
* | |
* Parameters: | |
* $num - your decimal integer | |
* $base - base to which you wish to convert $num (leave it 0 if you are providing $index or omit if you're using default (62)) | |
* $index - if you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "zyxwvu") | |
*/ | |
function dec2any($num, $base=62, $index=FALSE) { | |
if (!$base) { | |
$base = strlen($index); | |
} else if (!$index) { | |
$index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base); | |
} | |
$out = ""; | |
for ($t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t--) { | |
$a = floor( $num / pow( $base, $t ) ); | |
$out = $out . substr( $index, $a, 1 ); | |
$num = $num - ( $a * pow( $base, $t ) ); | |
} | |
return $out; | |
} | |
/* | |
* Custom > Decimal | |
* | |
* Parameters: | |
* $num - your custom-based number (string) (ex.: "11011101") | |
* $base - base with which $num was encoded (leave it 0 if you are providing $index or omit if you're using default (62)) | |
* $index - if you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "abcdef") | |
*/ | |
function any2dec($num, $base=62, $index=FALSE) { | |
if (!$base) { | |
$base = strlen($index); | |
} else if (!$index) { | |
$index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base); | |
} | |
$out = 0; | |
$len = strlen($num) - 1; | |
for ($t = 0; $t <= $len; $t++) { | |
$out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t ); | |
} | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment