How to encode and decode Base62
- Type or paste text into Input · Text. The Base62 string appears in Output · Base62.
- Set Alphabet under the output to match the system you are talking to: 0-9 A-Z a-z or 0-9 a-z A-Z.
- For a numeric ID, change From to Integer (big number) and type the number in decimal. For raw bytes, choose Hex or Base64.
- To decode, click Base62 → Text in the direction toggle. The Alphabet choice then sits under the input, and it must be the same one used for encoding.
Two alphabet orders with different results
Base62 has no RFC, and the 62 characters are ordered two ways in the wild. This converter offers both:
| Alphabet | Value of A | Value of a | Hello | 1000000 as a number |
|---|---|---|---|---|
0-9 A-Z a-z | 10 | 36 | 5TP3P3v | 4C92 |
0-9 a-z A-Z | 36 | 10 | 5tp3p3V | 4c92 |
The first order follows ASCII order, so encoded strings of equal length sort the same way as the numbers. GMP uses it for bases 37 to 62. The second order matches what you get when you extend hexadecimal’s lower-case a-f; some libraries call it the “inverted” alphabet.
Each output is the other with the case of every letter flipped. That makes the mistake easy to miss: decoding 5tp3p3V with the wrong order succeeds, but gives the bytes 4e 08 14 29 1d instead of Hello. If a decoded value looks like garbage, try the other order before suspecting the data.
Base62 for short IDs and URLs
URL shorteners and ID schemes use Base62 because every character is safe in a URL path, a file name and a double-click selection, and the result is short. A database ID is a number, so encode it from Integer (big number):
| Decimal | Base62 (0-9A-Za-z) |
|---|---|
125 | 21 |
3843 | zz |
3844 | 100 |
1000000 | 4C92 |
123456789 | 8M0kX |
Seven characters cover numbers up to 62^7 − 1, about 3.5 trillion. For a random ID, encode random bytes: a 16-byte UUID becomes 21 or 22 characters, against 32 in hex.
Short sequential IDs reveal how many records exist and are easy to guess. If that matters, encode random bytes rather than a counter.
How Base62 handles bytes and leading zeros
Because 62 is not a power of two, Base62 cannot map bit groups to characters. It reads the input as one big-endian number and converts that number to base 62. Leading zero bytes would disappear, so this tool writes one 0 digit for each of them: the bytes 00 00 encode as 00, and 00 ff as 047.
Libraries differ here too. Many encode only the number and drop leading zeros, and fixed-width formats pad to a set length instead. If the receiving side expects a fixed width, left-pad the output with 0. The big-number approach also has a cost that grows with the square of the input length, so this tool limits Base62 input to 64 KB.
Base62 in Python
Python has no Base62 in the standard library. For integer IDs, the conversion is a few lines:
ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def b62encode(n: int) -> str:
if n == 0:
return ALPHABET[0]
out = ""
while n:
n, r = divmod(n, 62)
out = ALPHABET[r] + out
return out
def b62decode(s: str) -> int:
n = 0
for ch in s:
n = n * 62 + ALPHABET.index(ch)
return n
b62encode(1000000) # '4C92'
b62decode("8M0kX") # 123456789
Swap the two letter blocks in ALPHABET for the other order. For bytes, convert with int.from_bytes(data, "big") first. More on byte handling in Python.
FAQ
What is the Base62 alphabet?
The ten digits plus the 26 upper-case and 26 lower-case letters: 62 characters, nothing else. The digits always come first, but libraries disagree on whether upper case (0-9A-Za-z) or lower case (0-9a-zA-Z) comes next.
What is the difference between Base62 and Base64?
Base64 adds + and / (or - and _) and = padding, and maps fixed 6-bit groups. Base62 uses only letters and digits, so it needs no escaping anywhere, but 62 is not a power of two, so it has to treat the data as one big number. The output is about 1% longer than Base64.
How do I encode a number to Base62?
Set From to Integer (big number) and type the decimal number. 1000000 becomes 4C92 with the default alphabet. Encoding the number as text would encode its digit characters instead.
Is Base62 case-sensitive?
Yes. A and a are different digits, worth 10 and 36 in the default order. A system that lowercases URLs or IDs will corrupt Base62 strings; use Base36 if case can get lost.