/*
###########################################################################
This function will convert an arbitrary precision positive decimal integer
into its equivalent in any given base from 2 to 36.
ARGUMENTS:
$Base10IntStr = Base-10 integer string to be to converted
into its BaseB equivalent.
$BaseB = Base of converted output (2 to 36).
ERRORS:
FALSE is returned if either of the arguments is non-numeric or outside of
valid range.
NO DEPENDENCIES
###########################################################################
*/
function BC_Base_10_Int_To_Base_B ($x10IntStr, $BaseB)
{
// ---------------------------------------
// Error if either argument is non-numeric
// or if base of out of range (2 to 36).
if (!is_numeric($x10IntStr) or !is_numeric($BaseB)) {return FALSE;}
if ($BaseB < 2 or $BaseB > 36) {return FALSE;}
// ------------------------------------------
// Define available digits for bases 2 to 36.
$BaseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$w = '';
$x = trim($x10IntStr);
// Remember any numerical sign and work with absolute value.
// The numerical sign will be restored at the end.
$sign = (substr($x, 0,1) == '-')? '-' :'';
if ($sign == '-') {$x = substr($x, 1, strlen($x));}
if (bccomp($x, "0") == 0) {return "0";}
while (bccomp($x, "0") > 0)
{
$w = substr($BaseDigits, bcmod($x, $BaseB), 1) . $w;
$x = bcdiv($x, $BaseB);
}
return $sign . $w;
} // End of BC_Base_10_Int_To_Base_B (...)