How Base64 works: bits, padding, and URL-safe encoding
Understand Base64 step by step: turn bytes into six-bit groups, decode padding, calculate size overhead, and choose the right alphabet.
Why represent bytes as text?
A photo, recording, or compressed file contains arbitrary byte values. Some systems accept text fields but have no place for raw binary. Base64 gives those bytes a reversible text representation. A JSON field can carry it without inventing an escaping scheme for every possible byte.
The tradeoff is size. Base64 does not compress, encrypt, or describe the content. Anyone with the encoded value can recover the bytes. If your protocol accepts a binary body or multipart file upload, encoding may be unnecessary.
A worked example: Man becomes TWFu
There are 64 alphabet symbols, so each symbol represents six bits: 2⁶ = 64. Three input bytes provide 24 bits, which split into four groups of six. For ASCII text Man:
Input bytes M = 77 a = 97 n = 110
Eight-bit groups 01001101 01100001 01101110
Six-bit groups 010011 010110 000101 101110
Decimal indices 19 22 5 46
Base64 symbols T W F u
Man → TWFu
The standard alphabet assigns indices 0–25 to A–Z, 26–51 to a–z, 52–61 to 0–9, then 62 to + and 63 to /. Decoding reverses the lookup and joins the bits back into bytes. See RFC 4648 §4.
What the equals signs mean
When the input ends with fewer than three bytes, the encoder fills the last six-bit group with zero bits. In padded Base64, = completes the four-character output block; it is not a 65th data symbol.
| Input | Bytes | Base64 | Padding |
|---|---|---|---|
M | 1 | TQ== | Two equals signs |
Ma | 2 | TWE= | One equals sign |
Man | 3 | TWFu | None |
For n input bytes, padded output length is 4 × ceil(n / 3). Three MiB becomes four MiB before any JSON or Data URL prefix. The familiar “about 33% larger” describes larger inputs; a one-byte input becomes four characters. Empty input stays empty.
Canonical encodings require zero unused pad bits. A permissive decoder accepting a value does not prove it has the one expected spelling. Follow the enclosing protocol for whether padding may be omitted and whether whitespace is accepted.
Text must become bytes first
Base64 has no concept of Chinese characters, emoji, or text encoding. Convert text to bytes using an agreed encoding such as UTF-8, then encode those bytes. After decoding, interpret the result as text only if you know its encoding. Images and audio should remain bytes.
import base64
text = "Hello, 世界 👋"
raw = text.encode("utf-8")
encoded = base64.b64encode(raw).decode("ascii")
restored = base64.b64decode(encoded, validate=True)
assert restored == raw
assert restored.decode("utf-8") == text
print(encoded)
Copying an invisible newline changes the input. This is why printf %s hello and a command that appends a newline can produce different encodings for what looks like the same word.
Standard Base64, Base64URL, and MIME
Base64URL substitutes - and _ for + and /. It is useful in URLs and tokens, but the alphabet choice alone does not decide whether padding is present. MIME has separate line-wrapping conventions. Choose the variant required by the consumer rather than deleting characters until a decoder accepts the input.
A Data URL such as data:image/png;base64,… adds a media type and a prefix around the encoded payload. That prefix is not part of the Base64 alphabet. For configuration details, see our standards guide and Base64 versus Base64URL guide.
Verify the transformation yourself
Open the text converter and try M, Ma, and Man. Decode each result and compare the recovered input. Next try a Unicode string with UTF-8 selected. For a binary file, compare bytes rather than how the output looks in a text editor.
Continue with runnable Python examples or see how these same bytes travel through AI speech workflows.