What Is Base64 Encoding and Decoding?

Base64 represents bytes with a text alphabet so they can travel through text-oriented formats. Encode bytes first, then decode them back to those same bytes. When the original data is text, both sides must agree on its character encoding.

Round-trip a UTF-8 string

In the example, café becomes five UTF-8 bytes because the accented letter occupies two. Python encodes the text to bytes, applies Base64, and converts the ASCII output to a string. Reverse those operations to recover the original text.

Base64 takes groups of three input bytes and emits four characters. Padding can mark an incomplete final group in formats that require it. This expansion is useful for transport, but it adds size. Keep large binary payloads in file or binary workflows when the receiving protocol supports them.

Python
import base64

encoded = base64.b64encode("café".encode("utf-8")).decode("ascii")
print(encoded)
print(base64.b64decode(encoded, validate=True).decode("utf-8"))
Result
Y2Fmw6k=
café

Choose the right alphabet

Standard Base64 uses plus and slash. Base64URL replaces them with hyphen and underscore. Follow the receiving specification for padding. JWT segments use the URL-safe alphabet without padding, while MIME transfers commonly use standard Base64 and may wrap long lines.

When extracting encoded data from a data URL, distinguish the media type and Base64 marker from the encoded bytes following the comma. A text decoder can display UTF-8 payloads. A decoded PNG or compressed archive requires a file-oriented tool or binary parser.

Investigate a decode mismatch

Test a short known string first. Check the alphabet, padding and whether the copied value includes literal quotes. If the decoded text shows unexpected characters, confirm the original encoding. If a signature or checksum differs, compare bytes, including trailing newlines.

Base64 is reversible without a key. Treat encoded secrets with the same care as their decoded forms. It is also different from percent encoding, which escapes URL components.

Frequently asked questions

Is Base64 encryption?
No. The text representation can be decoded by anyone who has it.
Can I decode a binary image here?
The browser tool displays UTF-8 text. Use an image or file decoder when the underlying bytes are binary.

Try it with these tools

Related developer guides