Python base64.b64encode
Base64 represents a sequence of bytes as ASCII text that a text-only field can carry.
A simple example
base64.b64encode
- Start with "cat" encoded as UTF-8
- Do this Encode its three bytes as Base64
- Result "Y2F0"
Syntax
base64.b64encode(b"tea").decode("ascii")
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyEncode a short delivery note for a text field
A system can carry a short payload only in a text field. Encode the original bytes into the alphabet the receiving field accepts, and distinguish that transport encoding from the underlying data.
import base64
print(base64.b64encode(b"tea").decode("ascii"))
dGVh
The three UTF-8 bytes for tea become the four Base64 characters dGVh. Decoding them recovers the original bytes.
Encode a parcel marker
A parcel terminal needs a text version of its three marker bytes. Encode the supplied ASCII bytes and read the four-character Base64 group. These inputs occupy a complete three-byte block.
Store an exhibit tag
A catalog stores three literal tag bytes in a Base64 field. Encode the supplied ASCII bytes and read the four-character Base64 group. These inputs occupy a complete three-byte block.
What to watch for
Base64 is reversible encoding. It offers no confidentiality.