Base36

Not to be confused with Base 36.

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 need less than 6 bits of information to be represented.

Conversion

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.

Java implementation

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

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

C++ implementation

template<typename IntegerT = unsigned int>
std::string to_base36(IntegerT val)
{
	static std::string 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

Swift implementation

extension IntegerType {
    // can convert any integer type to any base (2–36)
    func toBase(b:Int) -> String
    {
        guard b > 1 && b < 37 else {
            fatalError("base out of range")
        }
        let digits = ["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"]
        var result = ""
        
        if let v = self as? Int {
            var value = abs(v)
            repeat {
                result = digits[value % b] + result
                value = value / b
            } while (value > 0)
        }
        return self > 0 ? result : "-" + result
    }
}

Uses in practice

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 "Archived copy". Archived from the original on 2008-12-02. Retrieved 2009-05-06.

External links

This article is issued from Wikipedia - version of the 11/4/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.