Jump to content

Base36: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
C# Conversion Code: Fixed bug in Base64Encode when input value is 0
Line 193: Line 193:
char[] base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
char[] base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
string returnValue = "";
string returnValue = "";
while (value != 0)
do {
{
returnValue = base36Chars[value % base36Chars.Length] + returnValue;
returnValue = base36Chars[value % base36Chars.Length] + returnValue;
value /= 36;
value /= 36;
}
} while (value != 0);
return returnValue;
return returnValue;
}
}

Revision as of 02:36, 21 June 2009

Base 36 is a positional numeral system using 36 as the radix. 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.[1] Base 36 is therefore the most compact case-insensitive alphanumeric numeral system using ASCII characters, although its radix economy is poor. (Compare with base 16 and base 64.)

From a mathematical viewpoint, 36 is a convenient choice for a base in that it is divisible by both 2 and 3, and by their multiples 4, 6, 9, 12 and 18. Each base 36 digit can be represented as two base 6 digits.

The most common latinate name for base 36 seems to be hexatridecimal, although sexatrigesimal would arguably be more correct. The intermediate form hexatrigesimal is also sometimes used. For more background on this naming confusion, see the entry for hexadecimal. Another name occasionally seen for base 36 is alphadecimal, a neologism coined based on the fact that the system uses the decimal digits and the letters of the Latin alphabet.

Examples

Conversion table:

Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Base 36 0 1 2 3 4 5 6 7 8 9 A B C D E F G H
 
Decimal 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Base 36 I J K L M N O P Q R S T U V W X Y Z


Some numbers in decimal and base 36:

Decimal Base 36
1 1
10 A
100 2S
1,000 RS
10,000 7PS
100,000 255S
1,000,000 LFLS
1,000,000,000 GJDGXS
1,000,000,000,000 CRE66I9S
Base 36 Decimal
1 1
10 36
100 1,296
1000 46,656
10000 1,679,616
100000 60,466,176
1000000 2,176,782,336
10000000 78,364,164,096
100000000 2,821,109,907,456
Fraction Decimal Base 36
1/2 0.5 0.I
1/3 0.333333333333… 0.C
1/4 0.25 0.9
1/5 0.2 0.777777777777…
1/6 0.166666666666… 0.6
1/7 0.142857142857… 0.555555555555…
1/8 0.125 0.4I
1/9 0.111111111111… 0.4
1/10 0.1 0.3LLLLLLLLLLL...


Conversion

32- and 64-bit integers will only hold up to 6 or 12 base-36 digits, respectively. For numbers with more digits, one can use the functions mpz_set_str and mpz_get_str in the GMP arbitrary-precision math library. For floating-point numbers the corresponding functions are called mpf_set_str and mpf_get_str.

Python Conversion Code

def base36decode(input):
    CLIST="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    rv = pos = 0
    charlist = list(input)
    charlist.reverse()
    for char in charlist:
        rv += CLIST.find(char) * 37**pos
        pos += 1
    return rv

def base36decode_v2(input):
    return int(input,36)

def base36encode(input):
    if input == 0: return '0'
    CLIST="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    rv = ""
    while input != 0:
        rv = CLIST[input % 36] + rv
        input /= 36
    return rv

print base36decode("AQF8AA0006EH")
print base36encode(1412823931503067241)

C# Conversion Code

    public static string Base36Encode(Int64 value)
    {
        char[] base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
        string returnValue = "";
        do {
            returnValue = base36Chars[value % base36Chars.Length] + returnValue;
            value /= 36;
        } while (value != 0);
        return returnValue;
    }

    public static Int64 Base36Decode(string input)
    {
        string base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] arrInput = input.ToCharArray();
        Array.Reverse(arrInput);
        Int64 returnValue = 0;
        for (int i = 0; i < arrInput.Length; i++)
        {
            int valueindex = base36Chars.IndexOf(arrInput[i]);
            returnValue += Convert.ToInt64(valueindex * Math.Pow(36, i));
        }
        return returnValue;
    }

PHP Code

<?php
    $base_36 = "ZAQFG"; //Sample Base 36 Number
    $decimal = "7654321"; //Sample Decimal Number
    echo base_convert($base_36,36,10); //Outputs $base_36 converted to decimal
    echo base_convert($decimal,10,36); //Outputs $decimal converted to base 36
?>

Java Code

public class base36 {
    public static void main(String[] args) {
        System.out.println(Long.toString(72, 36)); // prints out "20"
    }
}
var dec=2353252;
document.write(dec.toString(36)+'<br>');// output: 1efs4

//convert reverse:
var base36='1efs4';
document.write(parseInt(base36,36));// output: 2353252

Ruby Code

1234567890.to_s(36) #kf12oi
"kf12oi".to_i(36) #1234567890

Uses in practice

  • The Remote Imaging Protocol for bulletin board systems used base 36 notation for transmitting coordinates in a compact form.
  • Many URL redirection systems like TinyURL or SnipURL/Snipr also use base 36 integers as compact alphanumeric identifiers.
  • Various systems such as RickDate use base 36 as a compact representation of Gregorian dates in file names, using one digit each for the day and the month.
  • Dell uses a 5 or 7 digit base 36 number (Service Tag) as a compact version of their Express Service Codes.
  • The software package SalesLogix uses base 36 as part of its database identifiers.[2]
  • The TreasuryDirect website, which allows individuals to buy and redeem securities directly from the U.S. Department of the Treasury in paperless electronic form, serializes security purchases in an account using a 4 digit base 36 number. However, the Latin letters A-Z are used before the Arabic numerals 0-9, so that the purchase are listed as AAAA, AAAB... AAAZ, AAA0, AAA1... AAA9, AABA...
  • The E-mail client program PMMail encodes the UNIX time of the email's arrival and uses this for the first six characters of the message's filename.
  • MediaWiki stores uploaded files in directories with names derived from the base-36 representation of a uploaded file's checksum [3].

References

  1. ^ Hope, Paco; Walther, Ben (2008), Web Security Testing Cookbook, Sebastopol, CA: O'Reilly Media, Inc., ISBN 978-0-596-51483-9
  2. ^ Sage SalesLogix base-36 identifiers: http://www.slxdeveloper.com/page.aspx?action=viewarticle&articleid=87
  3. ^ FileStore http://www.mediawiki.org/wiki/FileStore