codext defines a far broader set of Base-encodings than in the original library.


Classical base 2^N encodings

This namely adds the classical BaseXX encodings like 16 (hexadecimal) and 32 (RFC 3548), which are not available in the native codecs.

Common base encodings with N a power of 2:

Codec Conversions Aliases Comment
base2 text <-> Base2 encoded text (base[-_]?2|bin)-inv(erted)?
base4 text <-> Base4 encoded text base[-_]?4-inv(erted) charset: 1234
base8 text <-> Base8 encoded text base[-_]?8-inv(erted) charset: abcdefgh
base16 text <-> Base16 encoded text base[-_]?16-inv(erted)
base32 text <-> Base32 encoded text base[-_]?32-inv(erted)
zbase32 text <-> ZBase32 encoded text z[-_]?base[-_]?32 human-oriented Base32
base64 text <-> Base64 encoded text base[-_]?64-inv(erted)

Aliases

All the aliases are case insensitive for base encodings.

>>> codext.encode("test", "base2")
'01110100011001010111001101110100'
>>> codext.encode("test", "base2-inv")
'10001011100110101000110010001011'
>>> codecs.encode("this is a test", "base16")
'7468697320697320612074657374'
>>> codecs.decode("7468697320697320612074657374", "base16")
'this is a test'
>>> codecs.encode("this is a test", "base16-inv")
'1E02031DCA031DCA0BCA1E0F1D1E'
>>> codext.encode("this is a test", "base32")
'ORUGS4ZANFZSAYJAORSXG5A='
>>> codext.decode("ORUGS4ZANFZSAYJAORSXG5A=", "base32")
'this is a test'

Note that for base64, it overwrites the native base64_codec to also support en/decoding from str.

>>> codecs.encode("this is a test", "base64")
'dGhpcyBpcyBhIHRlc3Q='
>>> codecs.decode("dGhpcyBpcyBhIHRlc3Q=", "base64")
'this is a test'

Generic base encodings

Codec Conversions Aliases Comment
base3 text <-> Base3 encoded text base[-_]?36(|[-_]inv(erted)?)
base36 text <-> Base36 encoded text base[-_]?36(|[-_]inv(erted)?)
base58 text <-> Base58 encoded text base[-_]?58(|[-_](bc|bitcoin|rp|ripple|fl|flickr|short[-]?url|url)) supports Bitcoin, Ripple and short URL
base62 text <-> Base62 encoded text base[-_]?62(|[-_]inv(erted)?)
>>> codext.encode("test", "base3")
'23112113223321323322'
>>> codecs.encode("test", "base36")
'WANEK4'
>>> codecs.decode("4WMHTK6UZL044O91NKCEB8", "base36")
'this is a test'
>>> codext.encode("this is a test", "base58")
'jo91waLQA1NNeBmZKUF'
>>> codext.encode("this is a test", "base58-ripple")
'jo9rA2LQwr44eBmZK7E'
>>> codext.encode("this is a test", "base58-url")
'JN91Wzkpa1nnDbLyjtf'
>>> codecs.encode("test", "base62")
'289lyu'
>>> codecs.encode("this is a test", "base62")
'CsoB4HQ5gmgMyCenF7E'

Generic encodings

Base encodings are available for any N other than the ones explicitely specified using the "-generic" suffix. Their charsets consist of printable characters from the string module for N up to 100 and for characters composed from the 256 possible ordinals for a greater N.

:::python
>>> codext.encode("test", "base3-generic")
'12001002112210212211'
>>> codext.encode("test", "base17-generic")
'4cf60456'

Ascii85

This encoding relies on the base64 library and is only supported in Python 3.

Codec Conversions Aliases Comment
ascii85 text <-> ascii85 none Python 3 only
>>> codext.encode("this is a test", "ascii85")
"FD,B0+DGm>@3BZ'F*%"
>>> codext.decode("FD,B0+DGm>@3BZ'F*%", "ascii85")
'this is a test'
>>> with open("ascii85.txt", 'w', encoding="ascii85") as f:
    f.write("this is a test")
14
>>> with open("ascii85.txt", encoding="ascii85") as f:
    f.read()
'this is a test'

Other base encodings

Codec Conversions Aliases Comment
base85 text <-> Base85 encoded text base[-_]?85 Python 3 only (relies on base64 module)
base100 text <-> Base100 encoded text base[-_]?100|emoji Python 3 only

With Python 3, base85 and base100 (emoji's) are also supported.

>>> codecs.encode("this is a test", "base85")
'bZBXFAZc?TVIXv6b94'
>>> codecs.decode("bZBXFAZc?TVIXv6b94", "base85")
'this is a test'
>>> codecs.encode("this is a test", "base100")
'👫👟👠👪🐗👠👪🐗👘🐗👫👜👪👫'
>>> codecs.decode("👫👟👠👪🐗👠👪🐗👘🐗👫👜👪👫", "base100")
'this is a test'