Encodings

Base85

Four bytes as five characters in the RFC 1924 or Ascii85 or Z85 alphabet. Python b85encode and PDF streams and ZeroMQ
IDbase8510 / 20base85 · 85 characters
Encoding / Base85

Base85

Four bytes as five characters, in three alphabets.

Overhead
+25% on random bytes
Padding
none
Standard
RFC 1924 alphabet in git's grouping, Adobe Ascii85, ZeroMQ RFC 32/Z85
Used by
Python, git patches, PDF, ZeroMQ
family 1 / 20

Sample

Out
Xk~0{Zy<MXa%^NF
Back
"hello world!"

Options

alphabetstring
default rfc1924
rfc1924 (git, Python b85encode), ascii85 (! to u, z for zeros; PostScript, PDF) or z85 (ZeroMQ, whole 4-byte groups)
delimitersboolean
default false
Wrap the text in Adobe's <~ and ~>; ascii85 only

Access

Importimport { base85 } from "@agntn/encodings/base85"
CLIencodings encode base85 'hello world!'
Tryplayground with the sample above

RFC 1924 came out on 1 April 1996. It wanted every IPv6 address as one 128-bit number in base 85. Notice the date? Nobody writes IPv6 that way. The alphabet stuck anyway.

ts
encode("base85", "hello world");  // "Xk~0{Zy<MXa%^M"

This is the form people actually meet: four bytes as five characters, with a short last group cut. Python's b85encode writes it, and so does Mercurial. Git binary patches use the same alphabet and groups, with a length character in front of every line.

No z for zeros, no <~ ~>, and whitespace is an error. Python doesn't skip it either. Two more alphabets do the same arithmetic with other rules, and alphabet picks them. Decoding takes it too.

The ascii85 alphabet

Adobe's base85 from PostScript and PDF. Each group goes out as five digits from ! to u.

ts
const ascii85 = { alphabet: "ascii85" } as const;
encode("base85", "Hello, World!", ascii85);                       // "87cURD_*#4DfTZ)+T"
encode("base85", "Hello, World!", { ...ascii85, delimiters: true });  // "<~87cURD_*#4DfTZ)+T~>"
encode("base85", new Uint8Array(4), ascii85);                     // "z"

Four zero bytes are a single z, a small gift for sparse data. Decoding takes the <~ ~> wrapper or not, and skips whitespace. A z in the middle of a group is an error. Can five digits overflow 32 bits? uuuuu can, and it's refused. Only this alphabet has the wrapper, so delimiters anywhere else is an InvalidOptionError.

The z85 alphabet

ZeroMQ wanted base85 you can drop into a C string or a JSON value without escaping. So no ", no ', no backslash. XML is another story, < and & are both in there.

ts
base85.encode(hex.decode("864FD26FB559F75B"), { alphabet: "z85" });  // "HelloWorld"

That's the example from ZeroMQ RFC 32. Did they pick the bytes to spell it? Of course they did. Z85 only does whole groups. Encode two bytes and you get an error, not padding. Pad it yourself if you have to.