Base64 is everywhere in web development β in email attachments, data: URIs, JWT tokens, and image uploads β yet most developers have only a fuzzy idea of what it actually does. Here's the complete plain-English explanation: what the encoding is, how the byte math works, and why it makes data 33% bigger.
The one-line summary
Base64 converts binary data into a safe 64-character text alphabet (AβZ, aβz, 0β9, +, /) so it can travel through text-only channels like JSON, HTML, email, or URLs. Every 3 bytes of input become 4 text characters; if the input isn't divisible by three, padding = characters make up the difference.
1. Why 64 characters?
A byte is 8 bits and can hold 256 values (0β255). Many of those values are control characters, quotes, newlines, or non-printable β hostile to text protocols. Base64 uses the largest truly safe set: 26 uppercase + 26 lowercase + 10 digits + 2 symbols = 64 printable characters, which need only 6 bits each (2βΆ = 64).
Alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ Index 0-25 = A-Z, 26-51 = a-z, 52-61 = 0-9, 62 = '+', 63 = '/'
2. The math: 3 bytes β 4 characters
The encoder takes three 8-bit bytes (24 bits in total) and splits them into four 6-bit groups. Each 6-bit group (value 0β63) indexes the alphabet above:
Input: Man β bytes: 0x4D 0x61 0x6E Bits: 010011 010110 000101 101110 (24 bits β 4Γ6) Output: T W F u = "TWFu"
Because 24 is the least common multiple of 6 and 8, the math fits perfectly on three-byte boundaries β which is why output is always a multiple of 4 characters.
3. Why 33% overhead, and what about padding?
8 bits in β 6 bits out per character. The ratio is 8/6 = 4/3, i.e. every 3 input bytes produce 4 output characters, each storing only 6 bits of real data. That fixed 4/3 ratio is the famous 33.3% size increase (4/3 β 1).
When input length isn't a multiple of 3, the encoder pads:
| Input bytes | Bits | Base64 output | Padding |
|---|---|---|---|
| 3 (e.g. "Man") | 24 | TWFu | none |
| 2 (e.g. "Ma") | 16 | TWE= | 1Γ = |
| 1 (e.g. "M") | 8 | TQ== | 2Γ = |
The = tells decoders the final group is short. Some implementations strip padding (URL-safe JWT base64url) since = has special meaning in URLs.
4. Real-world use cases
- Email attachments (MIME) β the classic use; SMTP is text-only.
data:URIs in HTML/CSS β embed small images directly:<img src="data:image/png;base64,iVBORw0...">(no extra request).- JWT tokens β the header and payload are base64url-encoded JSON.
- JSON APIs β binary payloads (images, files) transported inside text JSON.
- "Basic" HTTP auth β
username:passwordbase64-encoded. - Cursor/opaque ids β compact strings without escaping headaches.
Encode text or files instantly β with UTF-8 support and URL-safe mode. Runs in your browser, nothing uploaded.
Open Base64 Encoder5. Common misconceptions
- "Base64 is encryption or compression" β No. It's a reversible encoding, publicly decodable within seconds; it adds 33%, never shrinks.
- "It makes data safer" β Only safe for text channels, not security. Never base64 for secrets.
- "More base64 = better" β For big binaries (e.g. 1 MB images), base64 inflates them 33% AND adds CPU cost; prefer real binary uploads.
- "Padding can be dropped freely" β Decoders usually cope, but keep
=for strict RFC 4648 tools.
6. Frequently asked questions
Is base64 case-sensitive? Yes β 'A' (index 0) and 'a' (index 26) decode to different bytes.
What characters does base64 use? AβZ, aβz, 0β9, +, /, and = for padding. The URL-safe variant swaps +β- and /β_.
How do I decode base64? Reverse the process: strip padding, map each character to its 6-bit index, reassemble bytes. Any decoder (like ours) does this instantly.
Can base64 represent arbitrary files? Yes β any byte sequence, including images, PDFs, and executable files, can be base64-encoded and transported as text.