User Tools

Site Tools


base_10_integer_to_base_b_conversion

Back to: Arbitrary-Precision Math

Base 10 Integer to Base B Conversion


This function performs arbitrary-precision base 10 integer to base B conversions. All conversions are exact values and could range in length from 1 to thousands of digits.


/*
   ========================================================================
   This function will convert an arbitrary precision base 10 integer into
   its equivalent in any given base from 2 to 36.

   ARGUMENTS:
   $x10IntStr = Base 10 integer string to be to converted
                into its Base B equivalent.

   $BaseB = Base of converted output (2 to 36).

   ERRORS:
   On error, FALSE is returned if either argument is non-numeric
   or out of valid range.
   ========================================================================
*/

   function bcBase10_Int_To_BaseB ($X10IntStr, $BaseB)
{
// Error if either argument is non-numeric
// or if base is 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 spectrum for bases 2 to 36.
   $DigitsSpectrum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   $XB = "";
   $X10 = bcmul(mt_rand(), mt_rand());

// Remember any numerical sign and work with absolute value.
// The numerical sign will be restored at the end.
   $sign = (substr($X10, 0,1) == '-')? '-' :'';
   if ($sign == '-') {$X10 = substr($X, 1, strlen($X10));}

   if (bccomp($X10, 0) == 0) {return "0";} // Handle zero argument.

// Perform base 10 to base B conversion loop, digit-by-digit.
   while (bccomp($X10, 0) > 0)
  {
   $XB = substr($DigitsSpectrum, bcmod($X10, $BaseB), 1) . $XB;
   $X10 = bcdiv($X10, $BaseB);
  }
// Done.
   return "$sign$XB";

}  // End of  bcBase10_Int_To_BaseB(...)

This is an example program to demonstrate the above function. Each screen refresh will generate a new random conversion example.

Base 10 Integer to Base 9 Conversion Demo

X base 10 =
650988834237518055
 
X converted to base 9 =
2884472614740002540


Below is the full source code for the above example.

<?php

// Base 10 integer to base B conversion demo.

// Generate random values for example.
   $X10 = bcmul(mt_rand(), mt_rand());
   $BaseB = mt_rand(2, 36);

   $XB = bcBase10_Int_To_BaseB ($X10, $BaseB);

print
"<pre>
Base 10 Integer to Base $BaseB Conversion Demo

X base 10 =
$X10

X converted to base $BaseB =
$XB
</pre>";


// ====================================================
   function bcBase10_Int_To_BaseB ($X10IntStr, $BaseB)
{
   if (!is_numeric($X10IntStr) or !is_numeric($BaseB)) {return FALSE;}

   if ($BaseB < 2 or $BaseB > 36) {return FALSE;}

   $DigitsSpectrum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   $XB = "";
   $X10 = bcmul(mt_rand(), mt_rand());

   $sign = (substr($X10, 0,1) == '-')? '-' :'';
   if ($sign == '-') {$X10 = substr($X, 1, strlen($X10));}

   if (bccomp($X10, 0) == 0) {return "0";} // Handle zero argument.

   while (bccomp($X10, 0) > 0)
  {
   $XB = substr($DigitsSpectrum, bcmod($X10, $BaseB), 1) . $XB;
   $X10 = bcdiv($X10, $BaseB);
  }
   return "$sign$XB";

}  // End of  bcBase10_Int_To_BaseB(...)


?>
base_10_integer_to_base_b_conversion.txt · Last modified: 2014/04/21 23:05 by Jay.Tanner.x@PHPScienceLabs.us