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); }
<?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";