Python has three sets of base 85 functions, in the base64
module.
base64.a85encode
and base64.a85decode
(Ascii85)base64.b85encode
and base64.b85decode
(Base85)base64.z85encode
and base64.z85decode
(Z85)These all convert bytes
objects to and from strings composed of 85(ish) printable ASCII characters.
What are their differences?
TL;DR just choose one for me: b85
.
The primary difference between these functions is the choice of character set. There are 94 non-whitespace printable ASCII characters:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Since we only need 85 characters, each function can exclude nine of them.
Ascii85 simply misses the nine highest bytes: vwxyz{|}~
. (It then uses z
for something else, which we'll see later.)
Base85 and Z85 take a different approach. Some characters are problematic in certain contexts, such as \
introducing escape sequences, or "
delimiting strings. These two try to skip characters that are most likely to be problematic.
Base85 excludes "',.:[]/\
and Z85 excludes "',;_`|~\
. Which of these is better depends on context.
Ascii85 has an additional feature: it compresses 4-byte all-zero sequences to z
(and optionally 4-byte all-space sequences to y
). This produces smaller encoded strings at the expense of some complexity. Its encoding and decoding functions also have a few extra options, but these don't look particularly useful.
In a quick benchmark, a Base85 round trip is about 10 % faster than Z85, which is about 30 % faster than Ascii85.
Z85 is new in Python 3.13, so don't use it if you need compatibility with older Python versions.