Base36

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

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).

Conversion

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 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.

C implementation

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);
}

The strdup() can be replaced by a strncpy(result_buffer, &buffer[offset], result_size) to avoid the memory allocation. The function arguments have to passe the char *result_buffer and size_t result_size.

C++ implementation

template<typename IntegerT = unsigned int>
std::string to_base36(IntegerT val)
{
	static char const base36[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	std::string result;
	result.reserve(14);
	do {
		result = base36[val % 36] + result;
	} while (val /= 36);
	return result;
}

C# implementation

public string ToBase36(uint value)
{
    char[] base36 = { '0','1','2','3','4','5','6','7','8','9','A',
                      'B','C','D','E','F','G','H','I','J','K','L',
                      'M','N','O','P','Q','R','S','T','U','V','W',
                      'X','Y','Z'};
    string result = "";

    do {
		result = base36[value % 36] + result;
	} while ((value /= 36) > 0);

    return result;            
}

bash implementation

value=$1
result=""
base36="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
while true; do
	result=${base36:((value%36)):1}${result}
	if [ $((value=${value}/36)) -eq 0 ]; then
		break
	fi
done
echo ${result}

Visual Basic implementation

Public Function ConvertBase10(ByVal d As Double, ByVal sNewBaseDigits As String) As String
    ' call using ConvertBase10(12345, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") for base36
    ' can be used to convert to any base
    ' from http://www.freevbcode.com/ShowCode.asp?ID=6604

    Dim S As String, tmp As Double, i As Integer, lastI As Integer
    Dim BaseSize As Integer
    BaseSize = Len(sNewBaseDigits)
    Do While Val(d) <> 0
        tmp = d
        i = 0
        Do While tmp >= BaseSize
            i = i + 1
            tmp = tmp / BaseSize
        Loop
        If i <> lastI - 1 And lastI <> 0 Then S = S & String(lastI - i - 1, Left(sNewBaseDigits, 1)) 'get the zero digits inside the number
        tmp = Int(tmp) 'truncate decimals
        S = S + Mid(sNewBaseDigits, tmp + 1, 1)
        d = d - tmp * (BaseSize ^ i)
        lastI = i
    Loop
    S = S & String(i, Left(sNewBaseDigits, 1)) 'get the zero digits at the end of the number
    ConvertBase10 = S
End Function

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.
  • Geohash-36, a coordinate encoding algorithm, uses radix 36 but uses a mixture of lowercase and uppercase alphabet characters in order to avoid vowels, vowel-looking numbers, and other character confusion.
  • 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 purchases 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 an uploaded file's checksum.[3]
  • Siteswap, a type of juggling notation, frequently employs 0–9 and a–z to signify the dwell time of a toss (which may roughly be thought of as the height of the throw). Throws higher than 'z' may be made but no notation has widespread acceptance for these throws.
  • In SEDOL securities identifiers, the check digit is computed from a weighted sum of the first six characters, each character interpreted in base-36.
  • In the International Securities Identification Number (ISIN), the check digit is computed by first taking the value of each character in base-36, concatenating the numbers together, then doing a weighted sum.
  • Reddit uses base-36 for identifying posts and comments.

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Sage SalesLogix base-36 identifiers: http://www.slxdeveloper.com/page.aspx?action=viewarticle&articleid=87
  3. FileStore http://www.mediawiki.org/wiki/FileStore

External links