What is Base64 encoding? A beginner-friendly guide
How binary data becomes printable text, why output is exactly one-third larger, and how to read a Base64 string — explained from first principles with a worked example, padding arithmetic, and comparisons to hex and Base32.
- fundamentals
- guide
Base64 is everywhere in web development — in JWTs, email attachments, inline images, API payloads — yet it’s frequently misunderstood. It’s not encryption, it’s not compression, and it doesn’t need a key. It’s a simple, reversible re-packaging of binary data as printable text. This guide explains exactly how it works, starting from the constraint that forces it to exist.
The problem Base64 solves
Computers move binary data — bytes — but many of the internet’s oldest protocols were designed for text: email (SMTP), JSON, XML, URLs. These channels either corrupt or reject bytes outside a safe printable range. If you paste raw binary into a JSON field, something will break: control characters get stripped, high-bit bytes get mangled by a legacy character set, or a parser simply refuses the input.
The constraint is older than the web. SMTP, specified in RFC 821 in 1982, carried messages as 7-bit ASCII lines. A PNG attachment is full of bytes with the high bit set — 0x89 in the magic number alone — and no line structure at all. Pump it through that channel raw and it arrives mangled. The fix that stuck was MIME (RFC 1341, later RFC 2045): encode arbitrary bytes as a restricted printable alphabet. PEM certificates were getting the same treatment around the same time (RFC 1421), and RFC 4648 (2006) finally wrote down the modern alphabets and rules in one place.
Base64 solves the general problem by mapping any sequence of bytes onto a 64-character alphabet that every text channel tolerates. 64 characters is a deliberate choice: 64 = 2⁶, so each character carries exactly 6 bits of your data — the largest power of two that maps onto a compact printable set.
The alphabet, position by position
The standard alphabet (RFC 4648 §4) is not arbitrary — order matters, because a character’s index in this string is the number its 6-bit group represents:
| Index range | Characters | Count |
|---|---|---|
| 0–25 | A–Z |
26 |
| 26–51 | a–z |
26 |
| 52–61 | 0–9 |
10 |
| 62 | + |
1 |
| 63 | / |
1 |
Index 19 is T, index 62 is +, and index 63 is /. This ordering is why decoding is a single table lookup — and why historical cousins like uuencode and BinHex, which shuffle the same idea with different alphabets, produce strings that no Base64 decoder can read.
How the encoding works, bit by bit
Take the string Man. In UTF-8 (and ASCII), that’s three bytes:
4D 61 6E
01001101 01100001 01101110
Base64 doesn’t look at the bytes as bytes — it looks at the whole bit stream and re-groups it into chunks of 6 bits instead of 8:
010011 010110 000101 101110
Each 6-bit chunk is a number between 0 and 63 — an index into the alphabet. 010011 is 19, and the 20th character (index 19) of ABCDEFGHIJKLMNOPQRSTUVWXYZ… is T. Working through all four chunks:
| 6-bit group | Decimal | Character |
|---|---|---|
010011 |
19 | T |
010110 |
22 | W |
000101 |
5 | F |
101110 |
46 | u |
So Man becomes TWFu. The bits never changed — they were just re-packaged from three 8-bit boxes into four 6-bit boxes.
Why the output is ~33% bigger
Three bytes in (24 bits) become four characters out (4 × 6 = 24 bits). Since a character is still stored as a full byte, you’ve turned 3 bytes into 4 — a fixed 33% overhead (exactly 33.3%, since the expansion is 4/3). That’s not a flaw; it’s the price of guaranteed safe transit through any text channel.
Concrete numbers make the ratio stick. A 1,000,000-byte file encodes to ⌈1,000,000 ÷ 3⌉ × 4 = 1,333,336 characters — including two padding characters, because 1,000,000 leaves a remainder of 1 when divided by 3. That makes it the worst case for padding, not for the ratio: padding never adds more than two characters, and the one-third expansion holds regardless — lengths divisible by three produce precisely one-third more. A hex dump of the same file would be 2,000,000 characters; Base32 would need 1,600,000. The size calculator on this site does this arithmetic for arbitrary inputs, including the formatting characters lines will add.
Padding handles inputs whose length isn’t a multiple of three. The rules fall directly out of the bit math:
| Bytes mod 3 | Leftover bits | Encoded chars | Padding | Output length mod 4 | Meaningless zero bits |
|---|---|---|---|---|---|
| 0 | 0 | (no stub) | — | 0 | 0 |
| 1 | 8 | 2 | == |
2 | 4 |
| 2 | 16 | 3 | = |
3 | 2 |
Those “meaningless zero bits” matter more than they look. In the mod-1 case, one byte (8 bits) fills one 6-bit group fully and contributes 2 bits to the second; the remaining 4 bits of that second character must be zero. RFC 4648 calls the padded form canonical and says decoders may reject a stub character whose stray bits aren’t zero — several strict implementations do exactly that, so a string whose stub bits are non-zero can decode cleanly on one platform and throw on another. When in doubt, paste both into the decoder: it reports non-canonical bits with their position instead of silently pretending they weren’t there.
It’s an encoding, not encryption
Anything Base64-encoded can be decoded by anyone, instantly, with no secret. Base64 provides compatibility, never confidentiality. If you need secrecy, encrypt first — then, if the channel is text-only, Base64-encode the ciphertext.
The same one-line dismissal applies to two adjacent misconceptions. Base64 does not compress: the output is always at least a third larger, so “compressing” with it is backwards — gzip is the tool that shrinks data, and you’d Base64-encode after gzipping (that ordering is exactly what a data URI of a compressed font does). And it’s not a hash: it has no fixed length, it’s lossless, and it’s trivially reversible — the properties a hash deliberately lacks. A separate post walks through why “it’s encoded” keeps fooling people who should know better, including devs publishing “hashed” passwords that decode to plaintext.
Where it shows up in the wild
- Data URIs (RFC 2397) inline any binary resource in one string:
data:image/png;base64,iVBORw0KGgo…. They’re the reasonsrc="…base64…"appears in HTML, CSS, JSON, and CSV exports. - PEM files wrap standard Base64 at 64 characters between
-----BEGIN PRIVATE KEY-----lines — 64 characters per line is exactly 48 decoded bytes. - JWTs concatenate three base64url segments (a different alphabet — see the dedicated post) with dots.
- MIME email still ships the vast majority of the world’s attachments in Base64, wrapped at 76 characters.
- Git treats binary files as opaque: a diff stops at
Binary files a/photo.jpg and b/photo.jpg differ, and a merge conflict on a binary file leaves.orig-style sidecar files instead of inline conflict markers — because the markers are text, too. Examine binary changes viagit diff --textonly at your own risk.
Once you can read a Base64 string as “6-bit groups of my original bytes,” the ~33% overhead, the padding rules, and even the URL-safe alphabet stop being trivia and become obvious. Try it: the encoder shows the intermediate bit groups as you type, the decoder has a hex/ASCII view of the raw bytes, and if you want the wider family — Base32, Base58, Base85, hex — the comparison post covers the trade-offs alphabet by alphabet.
FAQ
Why not just use hex? Hex doubles the data (4 bits per character); Base64 grows it by a third. A one-megabyte file is ~2 MB in hex but ~1.33 MB in Base64, cutting every text-only channel’s cost by a third. Hex retains a niche where per-byte readability wins — the base64-to-hex tool exists because spending readability for size is sometimes the right trade.
What does == at the end mean? Padding for a final stub of one byte: the two preceding data characters carry 12 bits, of which only the first 8 belong to data (see the table above). Stripped padding doesn’t change the data, only its length hint — which decoders can infer from length mod 4 instead.
Why is my Base64 full of line breaks? Because the RFC that made it mainstream (MIME, RFC 2045 §6.8) requires a line feed at least every 76 characters, and PEM (RFC 1421) standardized 64. Decoders must ignore the breaks; when a tool tracks the line length, that length is a very specific feature — 64 characters per line decodes to exactly 48 bytes of payload.
Is Base64 case-sensitive? Yes, acutely — TWFu and twfu encode to different bytes, since uppercase and lowercase occupy different index ranges. Any tool that lower-cases your input for convenience will silently corrupt binary data.
Try it yourself — right now
Both tools run 100% in your browser. Nothing you type or drop is ever uploaded.
Related posts
Related posts
October 28, 2026
Character Sets Explained: How Encoding Detection Works and Why It Matters for Base64
Base64 moves bytes, but characters become bytes through a character set. This guide explains ASCII, Unicode, UTF-8, legacy code pages, CJK and EBCDIC, how auto-detection works, and why picking the wrong charset turns readable text into mojibake.
October 26, 2026
Base64 data URLs: how to embed images in HTML, CSS, JSON and email
A complete, practical guide to data::URLs — when embedding an image inline with Base64 beats a regular request, the bandwidth trade-off nobody sells you, MIME types done right, CSP and email gotchas, and copy-paste syntax for HTML, CSS, SVG, JSON and Python.
October 20, 2026
How to Base64 encode and decode in JavaScript: btoa, atob, Buffer, and Data URLs
A complete, copy-paste-tested guide to Base64 in JavaScript: the btoa/atob basics, the Unicode trap that breaks naive code, TextEncoder done right, Node Buffer equivalents, and Data URL patterns for files.