Base62 Encoding Explained: The Go-To Scheme for Short URLs and Unique IDs
A comprehensive guide to Base62 encoding: how it works, its 62-character alphabet, encoding and decoding algorithms, and real-world applications in URL shortening, distributed ID generation, and more.
What is Base62?
Base62 is a binary-to-text encoding scheme that uses 62 printable characters to represent arbitrary binary data or integer values. These 62 characters consist of 10 Arabic digits (0-9), 26 uppercase letters (A-Z), and 26 lowercase letters (a-z) — the complete set of alphanumeric characters in ASCII.
The key advantage of Base62 is that it uses only letters and digits with no special symbols whatsoever. This makes it naturally suited for use in URLs, filenames, database primary keys, and all kinds of identifiers. In modern web development, Base62 has become a preferred encoding scheme for URL shortening services, distributed ID generators, and unique identifier systems.
Tool Recommendation: Need to encode or decode Base62? Try our online Base62 Encoder/Decoder with support for Text, Hexadecimal, and Numeric mode conversions in real time.
The Base62 Alphabet
Base62 uses the following 62 characters as its encoding alphabet:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Each character maps to an index value from 0 to 61:
| Range | Characters | Index Values |
|---|---|---|
| Digits | 0 - 9 | 0 - 9 |
| Uppercase | A - Z | 10 - 35 |
| Lowercase | a - z | 36 - 61 |
Note: Different implementations may use different character orderings. The order shown above (digits → uppercase → lowercase) is the most common standard and the one used by our tool. Some implementations place lowercase letters before uppercase — always verify which alphabet is in use.
How Base62 Works
Base62 encoding operates in two main modes depending on the type of input data: Numeric Mode (direct integer conversion) and Byte-Stream Mode (binary data encoding).
Numeric Mode (Integer ⇄ Base62)
This is the most straightforward mode — essentially a base conversion between decimal (base-10) and base-62.
Encoding Process:
- Repeatedly divide the decimal integer by 62
- Use each remainder as an index to select the corresponding character from the alphabet
- Reverse the collected characters to form the final result
Example: Convert the integer 123456789 to Base62
123456789 ÷ 62 = 1991238 remainder 33 → character 'X'
1991238 ÷ 62 = 32116 remainder 46 → character 'k'
32116 ÷ 62 = 518 remainder 0 → character '0'
518 ÷ 62 = 8 remainder 22 → character 'M'
8 ÷ 62 = 0 remainder 8 → character '8'
Reversed result: 8M0kX
Decoding Process (8M0kX → 123456789):
'8' = 8, 'M' = 22, '0' = 0, 'k' = 46, 'X' = 33
8 × 62⁴ + 22 × 62³ + 0 × 62² + 46 × 62¹ + 33 × 62⁰
= 118210688 + 5243216 + 0 + 2852 + 33
= 123456789
Byte-Stream Mode (Binary Data ⇄ Base62)
When encoding text strings or arbitrary binary data, Base62 uses a big-integer conversion approach:
- Convert data to a byte sequence: e.g., encode UTF-8 text into a byte array
- Treat the byte sequence as a big integer: concatenate all bytes in big-endian order into a single large integer
- Perform base-62 conversion: repeatedly divide by 62 and collect remainders
- Preserve leading zero information: each leading zero byte (0x00) in the input maps to the first character in the alphabet (
0)
Example: Encode the string “Hello”
1. UTF-8 bytes: 0x48 0x65 0x6C 0x6C 0x6F
2. Big integer: 310939249775 (decimal)
3. Base62 encoded: 5TP3P3v
Decoding reverses the process: convert the Base62 string back to a big integer, then split it into individual bytes.
Why Choose Base62?
1. URL Safety
Base62 uses only letters and digits — it contains none of the special characters found in URLs (such as +, /, =). Base62 strings can be embedded directly in URLs without any percent-encoding. This is a significant advantage over standard Base64.
2. Filesystem Compatibility
Base62-encoded strings can be used directly as filenames or directory names without worrying about OS restrictions on special characters.
3. High Encoding Efficiency
Among all purely alphanumeric encoding schemes, Base62 uses the most characters (62), achieving encoding efficiency close to the theoretical maximum. Compared to Base32 (32 characters), Base62 produces shorter strings; compared to Base36 (case-insensitive alphanumerics only), Base62 leverages case sensitivity to further reduce encoded length.
4. Double-Click Friendly
Base62 strings can be selected in their entirety with a double-click in most text editors and browsers, making copy-paste operations convenient.
5. Database Friendly
Purely alphanumeric strings are handled correctly by virtually every database, programming language, and framework without any need for escaping or special treatment.
Comparing Base62 with Other Encodings
| Feature | Base62 | Base64 | Base58 | Base36 | Base32 |
|---|---|---|---|---|---|
| Alphabet Size | 62 | 64 | 58 | 36 | 32 |
| Character Set | Digits + mixed-case letters | Digits + letters + +/ | Ambiguity-free alphanumerics | Digits + uppercase | Uppercase + digits |
| URL Safe | ✅ Natively | ❌ Needs variant | ✅ Safe | ✅ Safe | ✅ Safe |
| Case Sensitive | Yes | Yes | Yes | No | No |
| Special Symbols | No | Yes | No | No | No |
| Requires Padding | No | Yes (=) | No | No | Yes (=) |
| Expansion Ratio | ~37% | 33.3% | ~37% | ~54% | 60% |
| Primary Use | Short URLs, ID generation | General data transfer | Cryptocurrency | Case-insensitive IDs | 2FA, DNS |
Common Use Cases
1. URL Shortening Services
This is the classic Base62 application. The core idea:
- Store the long URL in a database and obtain an auto-incrementing ID
- Encode the ID as a Base62 string to use as the short code
- When the short link is visited, decode the short code, look up the original URL, and redirect
Example: Database ID 123456789 → Base62 → 8M0kX → Short URL https://short.url/8M0kX
Just 6 Base62 characters can represent 62⁶ ≈ 56.8 billion distinct links.
2. Distributed Unique IDs
In distributed systems, Base62 is commonly used to generate compact unique identifiers:
- Snowflake IDs: Encode 64-bit integers into ~11 Base62 characters
- UUID Compression: Compress 128-bit UUIDs from 32 hex characters to ~22 Base62 characters
- MongoDB ObjectId: 24-character hex IDs can be compressed into shorter Base62 representations
3. Session Tokens & API Keys
Base62-encoded random strings are frequently used as session tokens, API keys, invitation codes, and verification codes.
4. File Naming & Storage
Cloud storage services and CDNs often use Base62 encoding to generate unique filenames for uploaded files and identifiers for images and resources.
5. Database Sharding Keys
In database sharding scenarios, Base62-encoded IDs can be evenly distributed across shards while reducing primary key length and saving storage space.
Numeric Mode: Special Applications
Base62’s numeric mode (direct integer ⇄ Base62 conversion) can significantly shorten the string representation of large integers:
| Original Value | Decimal Length | Base62 Length | Compression |
|---|---|---|---|
| 1,000 | 4 chars | 2 chars | 50% |
| 1,000,000 | 7 chars | 4 chars | 43% |
| 2^53 (JS safe integer limit) | 16 chars | 9 chars | 44% |
| 2^63 (64-bit integer limit) | 19 chars | 11 chars | 42% |
Implementation Considerations
Pseudocode: Byte-Stream Encoding
function base62Encode(bytes):
x = bytesToBigInt(bytes) // Convert bytes to big integer
if x == 0:
return "0" × bytes.length // Handle all-zero input
result = []
while x > 0:
result.append(ALPHABET[x % 62]) // Map remainder to character
x = x ÷ 62
for each leading zero byte in bytes:
result.append('0') // Preserve leading zeros
return reverse(result)
Key Points
-
Leading Zero Handling: In byte-stream mode, leading zero bytes (0x00) are lost during big-integer conversion and must be preserved separately. This is a common trait of all big-integer-based encodings (e.g., Base58).
-
Big Integer Support: When processing longer data, intermediate values may exceed the range of standard integer types. Use
BigInt(JavaScript),math/big(Go), or equivalent big-integer libraries. -
Alphabet Consistency: Encoding and decoding must use the same alphabet. A different alphabet produces completely different results.
-
No Standard Padding: Unlike Base64, Base62 has no standardized padding mechanism. Different implementations may handle leading zeros and encoded length differently.
Advantages and Disadvantages
Advantages
- URL Safe: Purely alphanumeric — can be used in URLs without encoding
- Filesystem Safe: Can be used directly as filenames
- Compact & Efficient: Highest efficiency among purely alphanumeric encodings
- Double-Click Friendly: Entire strings can be selected with a double-click
- Cross-Platform Compatible: Handled correctly in any system and programming language
Disadvantages
- Case Sensitive: May cause collisions in case-insensitive environments (e.g., Windows filesystems) — consider Base36 in such cases
- Implementation Complexity: Relies on big-integer arithmetic, more complex to implement than Base64 (which uses bit operations)
- Performance Overhead: Big-integer division is slower than bit operations; not as fast as Base64 for large data
- No Official Standard: Unlike Base64 (RFC 4648), there is no official RFC for Base62
- No Built-in Checksum: Unlike Base58Check, there is no built-in error detection
Security Considerations
Although Base62 is widely used for generating tokens and identifiers, keep these points in mind:
- Base62 is not encryption: It is merely an encoding scheme and provides no security guarantees. Sensitive data should be encrypted before encoding.
- Randomness requirements: When used for tokens or keys, the underlying data must come from a cryptographically secure random number generator (CSPRNG), not simple auto-incrementing IDs.
- Length requirements: Ensure Base62 identifiers are long enough to resist brute-force guessing. For example, a 22-character Base62 string provides approximately 131 bits of entropy.
How to Choose the Right Encoding?
- Need URL safety with case sensitivity: Choose Base62 (short URLs, API keys)
- Need maximum encoding efficiency: Choose Base64 (data transfer, embedded data)
- Need human-friendly, error-resistant input: Choose Base58 (cryptocurrency addresses)
- Need case insensitivity: Choose Base36 or Base32
- Need maximum compactness: Choose Base85 or Base91
Conclusion
Base62 is a clean, efficient, and highly practical encoding scheme. By leveraging all 62 alphanumeric characters, it strikes an ideal balance between URL safety and encoding efficiency. From URL shortening services to distributed ID generation, from session tokens to file naming, Base62 is widely used across modern web development.
If you’re building a URL shortener, designing a distributed ID scheme, or need a compact and safe identifier encoding, Base62 is an excellent choice. Try our Base62 Encoder/Decoder to experience it firsthand!