Base62
The Base62 module implements encoding and decoding of binary data using only the 62 alphanumeric characters 0-9, A-Z, and a-z.
Important: This implementation uses the lexicographic (ASCII) order alphabet 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz, where value 0 maps to the character '0', not 'A' as in Base64. This is sometimes called the "ASCII-ordered" or "lex-ordered" variant. Other Base62 implementations may use different orderings (e.g., A-Za-z0-9 to align with Base64, or 0-9a-zA-Z).
Unlike Base64, Base62 does not use any padding characters (no =), and does not use non-alphanumeric symbols (no +, /, -, or _). This makes Base62 encoded strings safe for use in URLs, filenames, identifiers, and any context where only alphanumeric characters are permitted — without any escaping.
The encoding uses big-integer arithmetic with 32-byte chunking. Each chunk of up to 32 bytes is converted to a big integer and then encoded via repeated division by 62. Chunks are zero-padded (with '0') so that the output length is deterministic: it depends only on the input byte length, never on the content. This ensures that length(base62encode(data)) is constant for all data of a given length.
Whitespace characters (spaces, tabs, newlines) are ignored when decoding, matching the behavior of Base64 decoders per RFC 4648 §3.3.
The following alphabet is used:
| Value | Char | Value | Char | Value | Char | Value | Char |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 16 | G | 32 | W | 48 | m |
| 1 | 1 | 17 | H | 33 | X | 49 | n |
| 2 | 2 | 18 | I | 34 | Y | 50 | o |
| 3 | 3 | 19 | J | 35 | Z | 51 | p |
| 4 | 4 | 20 | K | 36 | a | 52 | q |
| 5 | 5 | 21 | L | 37 | b | 53 | r |
| 6 | 6 | 22 | M | 38 | c | 54 | s |
| 7 | 7 | 23 | N | 55 | t | 39 | d |
| 8 | 8 | 24 | O | 40 | e | 56 | u |
| 9 | 9 | 25 | P | 41 | f | 57 | v |
| 10 | A | 26 | Q | 42 | g | 58 | w |
| 11 | B | 27 | R | 43 | h | 59 | x |
| 12 | C | 28 | S | 44 | i | 60 | y |
| 13 | D | 29 | T | 45 | j | 61 | z |
| 14 | E | 30 | U | 46 | k | ||
| 15 | F | 31 | V | 47 | l |
Encoding
Base62.Base62EncodePipe — Type
Base62EncodePipe(ostream)Return a new write-only I/O stream, which converts any bytes written to it into base62-encoded ASCII bytes written to ostream. Calling close on the Base62EncodePipe stream is necessary to complete the encoding (but does not close ostream).
Base62 uses the lexicographic alphabet 0-9A-Za-z (62 alphanumeric characters). Note that value 0 maps to '0', not 'A' as in Base64.
Examples
julia> io = IOBuffer();
julia> iob62_encode = Base62EncodePipe(io);
julia> write(iob62_encode, "Hello!")
6
julia> close(iob62_encode);
julia> str = String(take!(io))
"0MbPS3UBt"
julia> String(base62decode(str))
"Hello!"Base62.base62encode — Function
base62encode(writefunc, args...; context=nothing)
base62encode(args...; context=nothing)Given a write-like function writefunc, which takes an I/O stream as its first argument, base62encode(writefunc, args...) calls writefunc to write args... to a base62-encoded string, and returns the string. base62encode(args...) is equivalent to base62encode(write, args...): it converts its arguments into bytes using the standard write functions and returns the base62-encoded string.
The optional keyword argument context can be set to a :key=>value pair or an IO or IOContext object whose attributes are used for the I/O stream passed to writefunc or write.
Base62 uses the lexicographic alphabet 0-9A-Za-z (62 alphanumeric characters). Note that value 0 maps to '0', not 'A' as in Base64.
See also base62decode.
Examples
julia> base62encode("Hello!")
"0MbPS3UBt"
julia> base62encode(UInt8[0x01, 0x02, 0x03])
"00HBL"
julia> base62encode(io -> print(io, "Hello!"))
"0MbPS3UBt"Decoding
Base62.Base62DecodePipe — Type
Base62DecodePipe(istream)Return a new read-only I/O stream, which decodes base62-encoded data read from istream.
Base62 uses the lexicographic alphabet 0-9A-Za-z (62 alphanumeric characters). Note that value 0 maps to '0', not 'A' as in Base64.
Examples
julia> io = IOBuffer("0MbPS3UBt");
julia> iob62_decode = Base62DecodePipe(io);
julia> String(read(iob62_decode))
"Hello!"Base62.base62decode — Function
base62decode(string)Decode the base62-encoded string and return a Vector{UInt8} of the decoded bytes.
Base62 uses the lexicographic alphabet 0-9A-Za-z (62 alphanumeric characters). Note that value 0 maps to '0', not 'A' as in Base64.
See also base62encode.
Examples
julia> b = base62decode("0MbPS3UBt")
6-element Vector{UInt8}:
0x48
0x65
0x6c
0x6c
0x6f
0x21
julia> String(b)
"Hello!"