Search code examples
pythonpython-3.4base85

What is the difference between a85encode and b85encode?


Python 3.4 added the a85encode and b85encode functions (and their corresponding decoding functions).

What is the difference between the two? The documentation mentions "They differ by details such as the character map used for encoding.", but this seems unnecessarily vague.


Solution

  • a85encode uses the character mapping:

    !"#$%&'()*+,-./0123456789:;<=>?@
    ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
    abcdefghijklmnopqrstu
    

    with z used as a special case to represent four zero bytes (instead of !!!!!).

    b85encode uses the character mapping:

    0123456789
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    !#$%&()*+-;<=>?@^_`{|}~
    

    with no special abbreviations.


    If you have a choice, I'd recommend you use a85encode. It's a bit easier (and more efficient) to implement in C, as its character mapping uses all characters in ASCII order, and it's slightly more efficient at storing data containing lots of zeroes, which isn't uncommon for uncompressed binary data.