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); }
/** * Converts a number from one base into another. May be called statically. * * @param mixed number The number to convert * @param int from_base The base to convert from * @param int to_base The base to convert to * @param string from_cs Optional character set of the number that is * converted * @param string to_cs Optional character set of the target number * @return string * @access public */ function baseConvert($number, $from_base, $to_base, $from_cs = null, $to_cs = null) { if (isset($this)) { $obj =& $this; } else { $obj =& Math_Basex::instance(); } if (!isset($from_cs)) { $from_cs = $obj->stdBase(); } if (!isset($to_cs)) { $to_cs = $obj->stdBase(); } if (strlen($from_cs) < $from_base) { return PEAR::raiseError('Character set isn\'t long enough for the' . 'given base.'); } if (strlen($to_cs) < $to_base) { return PEAR::raiseError('Character set isn\'t long enough for the' . 'given base.'); } $from_cs = substr($from_cs, 0, $from_base); $to_cs = substr($to_cs, 0, $to_base); if ($tmp = $obj->setBase($from_cs) !== true) { return $tmp; } $number = $obj->toDecimal($number); if (PEAR::isError($number)) { return $number; } if ($tmp = $obj->setBase($to_cs) !== true) { return $tmp; } $number = $obj->toBase($number); return $number; }
<?php define('MATH_BASEX_MATHEXTENSION', 'none'); include_once "Math/Basex.php"; // PHASE 1: Simple toBase and toDecimal calls $base = new Math_Basex("ABCDEF"); echo "Using character set: 'ABCDEF'\n"; echo validateResult("toBase(123456)", $base->toBase(123456), "CDFBDCA"); echo validateResult("toDecimal(\"BADA\")", $base->toDecimal("BADA"), "234"); echo "\n\n"; //PHASE 2: Testing int2 $base->setBase("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); echo "Changing to character base 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n"; echo validateResult("int2 (Max 65536) toBase(65432)", $base->toBase(65432), "DSUQ"); echo validateResult("int2 (Max 65536) tobase(67000) OVERFLOW!", $base->toBase(67000), "DVCY"); //PHASE 3: Testing int4 echo validateResult("int4 (Max 42944967297) toBase(64872767)", $base->toBase(64872767), "FLYZQL"); echo validateResult("int4 (Max 42944967297) tobase(43987654321) OVERFLOW!", $base->toBase(43987654309.0), "FMKGDIOB"); //PHASE $: Testing int8 echo validateResult("int8 (Max 18446744073709551616)" . " toBase(18446744073709551610)", $base->toBase(1844674409510065.0), "NBTNBSZVFVD"); echo validateResult("int8 (Max 18446744073709551616)" . " tobase(18446744073709551618) OVERFLOW!", $base->toBase("18446744098897893117"), "HLHXCZQBGYKMWB"); function validateResult($description, $result, $compareString) { if ((string) $result == (string) $compareString) { $ret = "{$description}\n - PASSED\n"; } else { $ret = "{$description}\n - FAILED - result was {$result}, " . "expecting {$compareString}\n"; } return $ret; }
<?php //include BaseX class include_once "Math/Basex.php"; //Create new instance $base6 = new Math_Basex("ABCDEF"); //just calling some methods for testing Basex class. echo "basex::toBase( 123456 ) --> " . $base6->toBase(123456) . " (Result: CDFBDCA)\n"; echo "basex::toDecimal( \"BADA\" ) --> " . $base6->toDecimal("BADA") . " (Result: 234)\n";