Base36

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

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[1] (the ISO basic Latin alphabet).

Each base36 digit needs less than 6 bits of information to be represented.

Conversion[edit]

Signed 32- and 64-bit integers will only hold at most 6 or 13 base-36 digits, respectively (that many base-36 digits overflow the 32- and 64-bit integers). For example, the 64-bit signed integer maximum value of "9223372036854775807" is "1Y2P0IJ32E8E7" in base-36. For example, the 32-bit signed integer maximum value of "2147483647" is "ZIK0ZJ" in base-36.

Standard implementations[edit]

Java SE supports conversion from/to String to different bases from 2 up to 36. For example, [1] and [2]

Just like Java, JavaScript also supports conversion from/to String to different bases from 2 up to 36. [3]

PHP, like Java, supports conversion from/to String to different bases from 2 up to 36. Use the base_convert function, available since PHP 4.

C implementation[edit]

static char *base36enc(long unsigned int value)
{
	char base36[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	/* log(2**64) / log(36) = 12.38 => max 13 char + '\0' */
	char buffer[14];
	unsigned int offset = sizeof(buffer);

	buffer[--offset] = '\0';
	do {
		buffer[--offset] = base36[value % 36];
	} while (value /= 36);

	return strdup(&buffer[offset]); // warning: this must be free-d by the user
}

static long unsigned int base36dec(const char *text)
{
	return strtoul(text, NULL, 36);
}

Python implementation[edit]

Python Code to encode BASE36 from BASE10[2]

 0 def base36encode(integer):
 1     chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 2     
 3     sign = '-' if integer < 0 else ''
 4     integer = abs(integer)
 5     result = ''
 6     
 7     while integer > 0:
 8         integer, remainder = divmod(integer, 36)
 9         result = chars[remainder]+result
10 
11     return sign+result

Visual Basic .NET implementation[edit]

Public Function ToBase36String(i as UInteger) As String
    Const rainbow = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim sb = New StringBuilder()
    Do
        sb.Insert(0, rainbow(i Mod 36))
        i /= 36
    Loop While i <> 0
    Return sb.ToString()
End Function

See also[edit]

References[edit]

  1. ^ Hope, Paco; Walther, Ben (2008), Web Security Testing Cookbook, Sebastopol, CA: O'Reilly Media, Inc., ISBN 978-0-596-51483-9
  2. ^ "2TechUp". 2TechUp. Missing or empty |url= (help)

External links[edit]