Created
January 31, 2016 03:34
-
-
Save nektro/7ed64bed93de345681dd to your computer and use it in GitHub Desktop.
PHP String base converter from https://secure.php.net/manual/en/function.base-convert.php
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
<?php | |
function convBase($numberInput, $fromBaseInput, $toBaseInput) | |
{ | |
if ($fromBaseInput==$toBaseInput) return $numberInput; | |
$fromBase = str_split($fromBaseInput,1); | |
$toBase = str_split($toBaseInput,1); | |
$number = str_split($numberInput,1); | |
$fromLen=strlen($fromBaseInput); | |
$toLen=strlen($toBaseInput); | |
$numberLen=strlen($numberInput); | |
$retval=''; | |
if ($toBaseInput == '0123456789') | |
{ | |
$retval=0; | |
for ($i = 1;$i <= $numberLen; $i++) | |
$retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i))); | |
return $retval; | |
} | |
if ($fromBaseInput != '0123456789') | |
$base10=convBase($numberInput, $fromBaseInput, '0123456789'); | |
else | |
$base10 = $numberInput; | |
if ($base10<strlen($toBaseInput)) | |
return $toBase[$base10]; | |
while($base10 != '0') | |
{ | |
$retval = $toBase[bcmod($base10,$toLen)].$retval; | |
$base10 = bcdiv($base10,$toLen,0); | |
} | |
return $retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment