Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convertions between decimal and base 36

Tags:

php

base36

I want to convert numbers in base 36 using PHP. The function base_convert is not working because I want to convert large numbers: I don't get my initial number if I convert it again from base 36 to decimal.

I tried some function given on multiple websites, but I never get the same result. Also, these two websites (in Javascript) give the same result:

  • http://www.unitconversion.org/numbers/base-10-to-base-36-conversion.html
  • http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/decimal-to-base-36/

For example 1010701001118000000000000000 has to be converted to 3IZS0ZE1RQ68W8SSW4.

Here are the functions I tried (and which don't work):

  • http://www.php.net/manual/fr/function.base-convert.php#34510
  • http://www.php.net/manual/fr/function.base-convert.php#106546
  • http://www.tonymarston.net/php-mysql/showsource.php?file=converter.php
  • http://www.geoffray.be/blog/php/convertir-des-nombres-en-base-62
like image 768
Yoone Avatar asked May 06 '12 16:05

Yoone


People also ask

What is base 36 called?

Base36 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-36 representation. The choice of 36 is convenient in that the digits can be represented using the Arabic numerals 0–9 and the Latin letters A–Z (the ISO basic Latin alphabet).

How do you convert a decimal to a different base?

Converting a decimal number to other base numbers is easy. We have to divide the decimal number by the converted value of the new base. Decimal to Binary Number: Suppose if we have to convert decimal to binary, then divide the decimal number by 2.

How do you convert a number from base to base?

Step 1 − Divide the decimal number to be converted by the value of the new base. Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 − Divide the quotient of the previous divide by the new base.

How do you convert base 10 to base 16?

Converting a base-10 number to a hexadecimal works much like converting a base-10 number to a binary number. You divide the number by the exponent (16) and keep dividing the result until the quotient is 0. At each step you multiply the remainder by 16 to get the hex value.


2 Answers

Here are two simple functions using an algorithm found on Wikipedia, while using bcmath to get the calculations even for very large numbers right:

function fromDecimalToBase($in, $to) {
    $in = (string) $in;
    $out = '';

    for ($i = strlen($in) - 1; $i >= 0; $i--) {
        $out = base_convert(bcmod($in, $to), 10, $to) . $out;
        $in = bcdiv($in, $to);
    }

    return preg_replace('/^0+/', '', $out);
}

function fromBaseToDecimal($in, $from) {
    $in = (string) $in;
    $out = '';

    for ($i = 0, $l = strlen($in); $i < $l; $i++) {
        $x = base_convert(substr($in, $i, 1), $from, 10);
        $out = bcadd(bcmul($out, $from), $x);
    }

    return preg_replace('/^0+/', '', $out);
}

However, I get 3izs0ze1rq66tifrpc for the number you've provided - maybe your conversion was wrong there?

like image 200
Niko Avatar answered Sep 30 '22 11:09

Niko


http://www.pgregg.com/projects/php/base_conversion/base_conversion.php

This page show you how to convert arbitrary length numbers between different bases. I tried your example and it looks like it works in both directions. Source code written in PHP is available.

like image 42
JK. Avatar answered Sep 30 '22 09:09

JK.