base64.a85encode / b85encode / z85encode
Three 85-character alphabets for the same idea: a 32-bit word becomes 5 base-85 digits, so the output is only 25% larger than the input. They are not interchangeable — decode with the function that encoded.
Demo
import base64 data = 'hello world'.encode() [base64.a85encode(data), base64.b85encode(data), base64.z85encode(data)]
All three use 5 characters per 4 bytes; a short last group is written with fewer characters (2 bytes → 3), so "hello world" (11 bytes) becomes 14 characters. Ascii85 shortens an all-zero group to z (and four spaces to y with foldspaces=True). The alphabets overlap, so the wrong decoder may succeed with garbage instead of failing — "HelloWorld" is the Z85 spec test vector, but all three accept it.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| b | bytes-like (encode) · bytes-like | ASCII str (decode) | yes | Data to encode, or text to decode. |
| foldspaces | bool | no (False) | Ascii85 only: write 4 spaces as "y" (a btoa extension, not Adobe). |
| wrapcol | int | no (0) | a85encode only: insert a newline every wrapcol characters; 0 means one line. |
| pad | bool | no (False) | a85encode / b85encode: pad the input with zero bytes to a multiple of 4 first (so the decoded result keeps the zeros). |
| adobe | bool | no (False) | Ascii85 only: frame the output with <~ and ~> (encode) or expect that framing (decode). |
| ignorechars | bytes | no (b' \t\n\r\x0b') | a85decode only: characters to skip — ASCII whitespace by default. |
Return value
bytes — Encode: ASCII bytes, 5 characters per 4 input bytes (fewer for a short last group unless pad=True). Decode: the original bytes.
Common patterns
import base64 encoded = base64.b85encode(blob)
import base64 stream = base64.a85encode(data, adobe=True, wrapcol=75) original = base64.a85decode(stream, adobe=True)
import base64 key_text = base64.z85encode(public_key) # 32 bytes → 40 characters
Examples
Pitfalls
import base64 base64.a85decode('<~BOu!rD]j7BEbo7~>')
import base64 base64.a85decode('<~BOu!rD]j7BEbo7~>', adobe=True)
import base64, binascii try: base64.b85decode('ab,c') except binascii.Error: print('bad input')
import base64 try: base64.b85decode('ab,c') except ValueError: print('bad input')
import base64 base64.b85decode(base64.z85encode(b'hi'))
import base64 base64.z85decode(base64.z85encode(b'hi'))
When to use
- PDF / PostScript streams (Ascii85, adobe=True)
- Git binary patches, Mercurial (Base85)
- ZeroMQ keys and messages (Z85, Python 3.13+)
- JSON, URLs, HTML or shell strings — all three alphabets contain < > &, and Ascii85 also quotes and backslash → Base64
- Interoperating with anything that just says "Base64" → b64encode
Notes
FAQ
Same arithmetic, different 85-character alphabets. Ascii85 uses ! to u (and z/y shortcuts); Base85 is the Git/Mercurial alphabet; Z85 is ZeroMQ's, chosen to be safe in source code strings. Their output is not interchangeable.