Knowledge
|

Base91 Encoding Explained: The Most Compact ASCII Binary-to-Text Encoding

A comprehensive guide to Base91 encoding: how its adaptive 13/14-bit bitstream algorithm works, its 91-character alphabet, encoding and decoding processes, efficiency comparisons with Base64 and Base85, and practical applications in data transport and storage.

What is Base91?

Base91 is a highly efficient binary-to-text encoding scheme designed by Joachim Henke. It uses 91 printable ASCII characters to represent arbitrary binary data, achieving the theoretical maximum compactness possible within the printable ASCII character range.

Compared to Base64 (33.3% overhead) and Base85 (25% overhead), Base91 achieves an average overhead of only 14%–23%, producing the shortest encoded output for the vast majority of data. This makes it the ideal choice for bandwidth-constrained, storage-sensitive, or text-embedded binary data scenarios.

Tool Recommendation: Need to encode or decode Base91? Try our online Base91 Encoder/Decoder with support for Text and Hex input, auto-conversion, and one-click result copying.

The Base91 Alphabet

Base91 uses 91 printable ASCII characters as its encoding alphabet. From the 94 printable ASCII characters in the range 33–126 (0x21–0x7E), it excludes three characters that commonly require escaping in programming languages and transport protocols — the hyphen - (ASCII 45), backslash \ (ASCII 92), and single quote ' (ASCII 39) — while retaining as many usable characters as possible:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"

A total of 91 characters, indexed from 0 to 90:

Index RangeCharacters
0–25AZ (uppercase letters)
26–51az (lowercase letters)
52–6109 (digits)
62–90!#$%&()*+,./:;<=>?@[]^_{|}~”` (special symbols)

Design Rationale: ASCII codes 33–126 contain 94 printable characters. After removing the hyphen -, single quote ', and backslash \, exactly 91 remain. And 91² = 8281 is just greater than the maximum value representable by 13 bits (8191), allowing two Base91 characters to fully represent any 13-bit value.

How Base91 Works

Base91 is fundamentally different from Base64 and Base85 — instead of fixed byte-group processing, it operates on a bitstream, outputting two Base91 characters at a time.

Encoding Process

  1. Build a bit queue: Push each input byte into a bit queue (least significant bit first)
  2. Extract 13 bits: When the bit queue accumulates more than 13 bits, extract the lowest 13 bits as a value v
  3. Conditional check:
    • If v > 88: Use these 13 bits as-is
    • If v ≤ 88: Extract one more bit (14 bits total) and use the 14-bit value as v
  4. Output two characters: Compute v % 91 and v / 91 (integer division) to get two index values, each mapped to a character in the Base91 alphabet
  5. Handle the tail: After all bytes are processed, if any bits remain in the queue, output 1 or 2 additional characters

Mathematical Basis:

  • 13 bits can represent a maximum value of 2¹³ − 1 = 8191
  • 91² = 8281 > 8191, so two Base91 characters can represent any 13-bit value
  • 14 bits can represent a maximum value of 2¹⁴ − 1 = 16383
  • The 14-bit path is only taken when the 13-bit value is ≤ 88, ensuring the combined value stays within the representable range of two Base91 characters

Encoding Example

Encoding the string "Hello":

Byte input:  H(0x48)  e(0x65)  l(0x6C)  l(0x6C)  o(0x6F)

Step 1: Push 0x48 = 01001000 into the bit queue
Step 2: Push 0x65 = 01100101 into the bit queue
        Bit queue now has 16 bits
Step 3: Extract low 13 bits → v = 0x6548 & 0x1FFF = 0x0548 = 1352
        1352 > 88 → use 13 bits
        1352 % 91 = 78  → alphabet[78] = '>'
        1352 / 91 = 14 → alphabet[14] = 'O'
        Output ">O", 3 bits remain in queue
... (subsequent bytes continue similarly)

Decoding Process

Decoding is the reverse of encoding:

  1. Read character pairs: Read two Base91 characters at a time, look up each character’s index value
  2. Reconstruct the value: v = first_index + second_index × 91
  3. Determine bit count:
    • If v & 8191 > 88: Write 13 bits into the bit queue
    • Otherwise: Write 14 bits into the bit queue
  4. Output bytes: Whenever the bit queue has 8 or more bits, extract the lowest 8 bits as a decoded byte
  5. Tail handling: If a single character remains at the end, use its index value to push bits directly into the queue

The Elegance of 13/14-Bit Adaptation

This adaptive mechanism is the core innovation of Base91:

  • When the 13-bit value is > 88, the 13 bits are sufficient to uniquely determine two Base91 characters (since the range 89–8191 is fully covered by 13 bits)
  • When the 13-bit value is ≤ 88, borrowing one extra bit expands to 14 bits, further increasing data density
  • This adaptive strategy brings the average encoded characters per byte close to the theoretical limit

Encoding Efficiency in Detail

Theoretical Analysis

Each pair of Base91 characters can encode 13 to 14 bits of data:

  • Best case: 14 bits of input produce 2 characters → overhead = 2 / (14/8) − 1 ≈ 14.3%
  • Worst case: 13 bits of input produce 2 characters → overhead = 2 / (13/8) − 1 ≈ 23.1%
  • Average case: For random data, the probability of the 13-bit value being ≤ 88 is only 89 / 8192 ≈ 1.1%, so the 13-bit path is taken almost always. The actual overhead is therefore very close to ~23%. The 14.3% best case is only achievable with specifically crafted input distributions where small 13-bit values occur frequently.

Efficiency Comparison with Other Encodings

SchemeInput : OutputOverheadAlphabet Size
Base91Adaptive bitstream~14–23%91
Base854 : 525%85
Base643 : 433.3%64
Base62Big integer~37%62
Base58Big integer~37%58
Base325 : 860%32
Base16 (Hex)1 : 2100%16

The larger the data, the more significant Base91’s advantage becomes. For kilobyte-scale data and above, it saves 10%–15% compared to Base64.

Practical Encoded Length Examples

Original DataBase64 LengthBase85 LengthBase91 Length
10 bytes16 chars13 chars12 chars
100 bytes136 chars125 chars116 chars
1,000 bytes1,336 chars1,250 chars1,154 chars
10,000 bytes13,336 chars12,500 chars11,534 chars

Alphabet Design Decisions

Why Exclude These Three Characters?

Excluded CharacterASCII CodeReason
Space 32Used as a delimiter in many protocols and formats; easily truncated or collapsed
Backslash \92Requires escaping in string literals of virtually all programming languages
Single quote '39Requires escaping in SQL queries, shell commands, and similar contexts

Differences from the Base85 Alphabet

Base85 (Ascii85) uses consecutive ASCII characters from code 33 to 117, while Base91 deliberately skips the space, single quote, and backslash, selecting a set of characters that can be used directly in more contexts. This means Base91-encoded output can be embedded as-is in most text containers — with the notable exception of JSON strings (which still require escaping the double quote ").

Common Use Cases

1. Efficient Data Embedding

When binary data needs to be embedded in plain-text environments (e-mail bodies, log entries, configuration file comments), Base91 provides the most compact encoding available. For every 1 KB of original data, Base91 saves approximately 100–180 bytes compared to Base64.

2. QR Codes and Barcodes

QR Code’s alphanumeric mode supports a limited character set. Encoding binary data with Base91 first allows packing more information into a single QR code — approximately 8%–15% more effective capacity than Base64.

3. Network Protocols and API Transport

When custom network protocols or REST APIs need to transmit binary payloads as text, Base91 reduces message size and bandwidth consumption. The difference is especially significant in bandwidth-constrained environments such as mobile networks.

4. Embedded Systems and IoT

In storage- and bandwidth-constrained IoT devices and embedded systems, Base91’s low overhead means firmware updates, sensor data uploads, and similar transmissions can be completed with fewer bytes.

5. Compression Pipeline Companion

While Base91 is not a compression algorithm, encoding already-compressed binary data with Base91 minimizes the overhead added by text encoding. The pipeline compress → Base91 encode → text transport → Base91 decode → decompress is the most space-efficient approach for text-channel binary transfer.

6. Database Text Field Storage

When binary data (small icons, signature blobs, serialized objects) needs to be stored in VARCHAR/TEXT database columns, Base91 can save approximately 8%–15% of storage space compared to Base64.

Detailed Comparison with Other Encodings

FeatureBase91Base85Base64Base62Base58
Alphabet Size9185646258
Overhead~14–23%25%33.3%~37%~37%
Encoding UnitBitstream4→53→4Big intBig int
Special SymbolsYesYesYes (+/=)NoNo
URL Safe❌ Needs variant
Fixed Ratio❌ Variable✅ 4:5✅ 3:4❌ Variable❌ Variable
StandardizedNoneAdobe standardRFC 4648NoneNone
Implementation ComplexityMediumHigherLowHigherHigher
Primary UseMax compactnessPS/PDF/GitGeneral transferShort URLs/IDsCryptocurrency

Implementation Considerations

  1. Bit Operation Precision: Base91 encoding and decoding rely on bit-level operations. In JavaScript, bitwise operators work on 32-bit integers, so the bit queue value must not exceed 32 bits. Practical implementations typically keep the bit queue within a safe range by extracting 13 or 14 bits promptly.

  2. Character Validation: During decoding, each input character must be verified against the 91-character alphabet. Two strategies exist for handling invalid characters: skip them (lenient mode) or throw an error (strict mode).

  3. Tail Character Handling: At the end of encoding, the bit queue may contain fewer than 13 residual bits. In this case, 1 character is output if the remaining bits are ≤ 7 and the value is ≤ 90, or 2 characters if the value exceeds 90 or more than 7 bits remain. The decoder must correctly handle odd-length input strings.

  4. Variable Output Length: Unlike Base64’s fixed 3:4 ratio or Base85’s 4:5, Base91’s output length depends on the data content. Pre-allocating output buffers requires estimating based on the worst case.

  5. Non-ASCII Compatibility: Base91-encoded output contains only ASCII printable characters and is fully compatible with UTF-8 environments. However, the encoded string includes the double quote " and other characters that require escaping in JSON contexts.

Advantages and Disadvantages

Advantages

  • Maximum Encoding Efficiency: The lowest overhead among all ASCII printable character encoding schemes
  • Thoughtful Character Selection: Excludes the three most problematic characters — space, backslash, and single quote
  • Streaming Friendly: The bitstream-based design naturally supports streaming encode/decode without knowing the total data length in advance
  • Self-Contained: No padding characters (like Base64’s =) are needed; the encoded length is naturally determined
  • Significant Space Savings: For large data volumes, the savings over Base64 are substantial

Disadvantages

  • No Formal Standard: No RFC or other official specification exists; subtle differences may exist between implementations
  • Contains Special Characters: Output includes HTML/XML-sensitive characters like <, >, ", and &
  • Not URL Safe: Contains URL-reserved characters; not suitable for direct use in URL parameters
  • Variable Length Output: Cannot predict output length as precisely as Base64
  • Limited Library Support: Not as universally supported as Base64 across programming languages
  • Debugging Inconvenience: Fewer widely available online tools compared to Base64

Security Considerations

  1. Encoding is not encryption: Like all encoding schemes, Base91 is merely a data representation change and provides no confidentiality. Sensitive data must be encrypted (e.g., AES, ChaCha20) before encoding.

  2. Injection risks: Base91 output contains characters like <, >, ", and &. Embedding it directly in HTML, XML, or SQL statements may lead to injection attacks. Always apply proper escaping in the relevant context.

  3. Data integrity: Base91 includes no built-in checksum or error-correction mechanism. If data integrity is required, add CRC32, SHA-256, or similar verification at the application level.

  4. Side-channel safety: For security-sensitive applications (such as encoding cryptographic keys), be aware of potential timing or memory-access-pattern side-channel leaks in the implementation.

How to Choose the Right Encoding?

  • Need maximum encoding efficiency, special characters acceptable: Choose Base91
  • Need high efficiency with industry-standard backing: Choose Base85 (Adobe PostScript/PDF)
  • Need universal compatibility and native support: Choose Base64 (RFC 4648, built into nearly every language)
  • Need URL safety and human friendliness: Choose Base62 or Base58
  • Need the simplest possible implementation: Choose Base16 (Hex)

Conclusion

Base91 is the most efficient binary-to-text encoding scheme available within the ASCII printable character range. Through its ingenious 13/14-bit adaptive bitstream algorithm, it compresses overhead to just 14%–23%, significantly outperforming both Base64’s 33.3% and Base85’s 25%. In data embedding, network transport, IoT devices, and other size-sensitive scenarios, Base91 is a compelling first choice.

Although Base91 lacks a formal standardization document and the ubiquitous native library support enjoyed by Base64, its exceptional encoding efficiency makes it irreplaceable in specific use cases. Try our Base91 Encoder/Decoder to experience this maximally compact encoding scheme firsthand!