Convert Very Large Decimal Integers to Base 62 And Back

The other day I was trying to devise a way of converting arbitrarily large base 10 integers to the order of a billion billion to base 62 and did not get much help from the web. So I devised my own solution using the BC Math extension which ships with PHP by default. So, this solution works upto PHP_INT_MAX which is 9223372036854775807 for 64-bit platform. It also works upto 99 trillion on 32-bit-platform. I used an array as a stack to store successive remainders of division by the base, which is 62, and at the end popped them one by one from the stack to generate the converted base 62 number. The converse, multiplication by successive powers of 62 and addition of those powers helped to convert base 62 back to base 10.

First I used PHP OOPS to create a class called MyBase62Converter. The code is given below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class MyBase62Converter
{
	private $base62StrVar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
 
	function encode($decNum, $base = 62)
	{
		$myStack = array();
 
		$remainder = 0;
 
		$base62Str = $this->base62StrVar;
 
		do 
		{
 
			$remainder = bcmod($decNum, $base);
 
			array_push($myStack, $remainder);
 
			$decNum = bcdiv(bcsub($decNum, $remainder), $base); 
 
		}
		while($decNum > 0);
 
		$binNum = '';
 
		while(count($myStack) > 0)
		{
 
			$binNum.= substr($base62Str, array_pop($myStack), 1);
		}
 
		return (strlen($binNum) >= 6)?$binNum: str_pad($binNum, 6, 0, STR_PAD_LEFT);
	}
 
	function decode($baseNum, $base=62)
	{
 
		$base62Str = $this->base62StrVar;
 
		$hashLen = strlen($baseNum);
 
		$decNum = '';
 
		for($i = 0; $i < $hashLen; $i++)
		{
			$decNum = bcadd($decNum, bcmul(bcpow($base, $i), strpos($base62Str , substr($baseNum, $hashLen - $i - 1, 1))));
		}
 
		return $decNum;
	}
 
}

Using the two simple encode and decode functions we can convert base 10 integers to base 62 representation and vice versa.

You can see the example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require_once("lib/class.MyBase62Converter.php"); 
 
$encoder = new MyBase62Converter();
 
echo "<br />";
 
echo PHP_INT_MAX." encoded is ".$encoded = $encoder->encode(PHP_INT_MAX, 62);
 
echo "<br />";
 
echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);
 
echo "<br /><br />";
 
$largeNum = bcpow(62, 6) - 1;
 
 
 
echo $largeNum." encoded is ".$encoded = $encoder->encode($largeNum, 62);
 
echo "<br />";
 
echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);
 
echo "<br /><br />";
 
$veryLargeNum = bcpow(62, 7) - 1;  //getting 0 due to num expressed in scientfic exponential notation 2.183401055849E+14
 
 
 
echo $veryLargeNum." encoded is ".$encoded = $encoder->encode($veryLargeNum , 62);
 
echo "<br />";
 
echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);
 
echo "<br /><br />";
 
/* */
 
$max_num = 99999999999999;
 
 
echo $max_num." encoded is ".$encoded = $encoder->encode($max_num , 62); //99999999999999
 
echo "<br />";
 
echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);
 
 
 
echo "<br /><br />";
 
/* */
 
$new_num = 63;
 
 
echo $new_num." encoded is ".$encoded = $encoder->encode($new_num , 62); //99999999999999
 
echo "<br />";
 
echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);

The result of code execution is given below:

One thought on “Convert Very Large Decimal Integers to Base 62 And Back

  1. Hi,

    I’ve been visiting your website a few times and decided to give you some positive feedback because I find it very useful. Well done.

    I was wondering if you as someone with experience of creating a useful website could help me out with my new site by giving some feedback about what I could improve?

    You can find my site by searching for “casino gorilla” in Google (it’s the gorilla themed online casino comparison).

    I would appreciate if you could check it out quickly and tell me what you think.

    casinogorilla.com

    Thank you for help and I wish you a great week!

Leave a Reply

Your email address will not be published. Required fields are marked *