User Tools

Site Tools


base_b_integer_to_base_10_conversion

Back to: Arbitrary-Precision Math

Base B Integer To Base 10 Conversion


This function will convert any base B integer into its corresponding equivalent in base 10. Since it is an arbitrary-precision function, it can handle integers with up to thousands of digits.

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

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

   $B = Base of input digit string (2 to 36).

   ERRORS:
   On error, FALSE is returned if either argument is invalid.

   ========================================================================
*/

   function bcBaseB_Int_To_Base10 ($XBaseBString, $BaseB)
{

// Read input arguments.
   $Xb = strtoupper(trim($XBaseBString));
   $B  = trim($BaseB);

// ERROR if base is outside of range 2 to 36.
   if ($B < 2 or $B > 36) {return FALSE;}

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

// Count digits in base b integer X.
   $XbDigitsCount = strlen($Xb);

// Initialize base 10 summation accumulator
// and initial power (= 1) of the base.
   $X10 = 0;
   $PowB = 1;

// Define the valid digits spectrum for the given base.
   $DigitsSpectrum = substr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $B);

// Perform loop to compute the base 10
// equivalent of the base b integer Xb.
   for ($i=0;   $i < $XbDigitsCount;   $i++)
       {
//      Get current base b digit from Xb.
        $db = substr($Xb, $XbDigitsCount - $i - 1, 1);

//      Get base 10 numerical value (= String index) of digit.
        $d10 = strpos($DigitsSpectrum, $db);

//      If FALSE is returned, it means that the base b digit was
//      not found within the valid base b DigitsSpectrum string.
        if ($d10 === FALSE) {return FALSE;}

//      Compute and accumulate value of current base 10 term.
        $X10 = bcadd($X10, bcmul($PowB, $d10));

//      Update power of base for next cycle.
        $PowB = bcmul($PowB, $B);
       }

// Done.
   return "$sign$X10";

} // End of  bcBaseB_Int_To_Base10(...)

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

Integer X Base B to base 10 example

X base 33 =
UH1DKO3LLCMMU51FW641F6L5S2MHUTPKV8P7UUANBM3N4SUJJJBD62JUC9F367MTN2MEO6BJVBKJ77334JAE0JIQ

X converted to base 10 =
39377375383542240585614095923034269844351138348804790290598117794356399135514941713910522548984998569618129580781631609490437160333538

Below is the full source code listing for the above example program. The program seems long winded because it also includes an extra utility function to generate the random numbers used for the example.

<?php

// Base B integer to base 10 conversion example/demo.

// Generate random values for the example.
   $B = mt_rand(2, 36);
   $digits = mt_rand(6, 112);
   
   $XB = Random_Digits_BaseB ($digits, $B, TRUE);
   
   $X10 = bcBaseB_Int_To_Base10($XB, $B);


print
"<pre>
Base B integer to base 10 example

X base $B =
$XB

X converted to base 10 =
$X10
</pre>";


// ==========================================================
   function bcBaseB_Int_To_Base10 ($XBaseBString, $BaseB)
{
   $Xb = strtoupper(trim($XBaseBString));
   $B  = trim($BaseB);

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

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

   $XbDigitsCount = strlen($Xb);

   $X10 = 0;
   $PowB = 1;

   $DigitsSpectrum = substr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $B);

   for ($i=0;   $i < $XbDigitsCount;   $i++)
       {
        $db = substr($Xb, $XbDigitsCount - $i - 1, 1);

        $d10 = strpos($DigitsSpectrum, $db);

        if ($d10 === FALSE) {return FALSE;}

        $X10 = bcadd($X10, bcmul($PowB, $d10));

        $PowB = bcmul($PowB, $B);
       }
   return "$sign$X10";

} // End of  bcBaseB_Int_To_Base10(...)


// ****************
// ****************

/*
   ========================================================================
   This function generates a string of any number of random digits in any 
   given base from 2 to 36.

   All letters (A to Z) used as digits will be returned in uppercase.

   ERRORS:
   On error, FALSE is returned for invalid argument(s).
   ========================================================================
*/

   function Random_Digits_BaseB ($NumDigits=1, $BaseB=10, $LZeroSuppressed=FALSE)
{
   $N = intval($NumDigits);
   $B = intval($BaseB);

   if ($B < 2 or $B > 36 or $N < 1) {return FALSE;}

   $DigitsSpectrum = substr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",0,$B);

   $RandomDigits = "";

   for ($i=0;   $i < $N;   $i++)
  {
   if (mt_rand() % 2 == 0)
  {$RandomDigits .= substr($DigitsSpectrum, mt_rand(0,$B-1),1);}
   else
  {$RandomDigits = substr($DigitsSpectrum, mt_rand(0,$B-1),1) . $RandomDigits;}
  }
   if ($LZeroSuppressed)
  {
   $RandomDigits = substr($DigitsSpectrum, mt_rand(1,$B-1),1) . $RandomDigits;
  }
   return substr("$Ldigit$RandomDigits", 0, $N);

}   

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