/** * Converts base of numbers. Supports base 2 to 36. * Note: Native support exists http://php.net/base_convert. * * @author Jitendra Adhikari <*****@*****.**> * * @param string $num Numeric string. * @param int $tobase To base * @param int $frombase From base (defaults to 10) * * @return string The number with converted base * * @throws InvalidArgumentException If input string or base is invalid */ function convert_base($num, $tobase, $frombase = 10) { if ($tobase == $frombase) { return $num; } $map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if ($tobase > strlen($map) or $tobase < 2 or $frombase > strlen($map) or $frombase < 2) { throw new \InvalidArgumentException('Given base not yet supported'); } // Validate $num if (trim(strtoupper($num), substr($map, 0, $frombase))) { throw new \InvalidArgumentException(sprintf('%s is not valid base %s number', $num, $frombase)); } // Adapt to base 10 (only for first go, not used by recursion) if (10 != $frombase) { $tmp = 0; foreach (str_split(strrev($num)) as $pow => $n) { $tmp += stripos($map, $n) * pow($frombase, $pow); } $num = $tmp; } if ($num < $tobase) { return $map[(int) $num]; } return convert_base(floor($num / $tobase), $tobase) . $map[$num % $tobase]; }
<?php //Include BaseX class include_once "Math/Basex.php"; //mirror HEX character set an convert current code to new code $newcode = convert_base("5c", "012345679abcdef", "fedcba9876543210"); echo $newcode . " (Result: a9)\n"; $newcode = Math_Basex::baseConvert("14", 10, 2); echo $newcode . " (Result: 1110)\n"; function convert_base($code, $oldbase, $newbase) { // take old base an input $base = new Math_Basex($oldbase); //convert code to base10 decimal number $number = $base->toDecimal($code); //change to the new base $base->setBase($newbase); //encode the decimal number and return the result to the function return $base->toBase($number); }
/** * @depends testConvertBaseException * @expectedException \InvalidArgumentException */ public function testConvertBaseException2() { convert_base('1234', 1, 10); }