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:

ValueCharValueCharValueCharValueChar
0016G32W48m
1117H33X49n
2218I34Y50o
3319J35Z51p
4420K36a52q
5521L37b53r
6622M38c54s
7723N55t39d
8824O40e56u
9925P41f57v
10A26Q42g58w
11B27R43h59x
12C28S44i60y
13D29T45j61z
14E30U46k
15F31V47l

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!"
source
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"
source

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!"
source
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!"
source