Base64 vs URL Encoding vs HTML Entities: Which One Do You Need?
All three are called "encoding", and all three turn awkward characters into safe ones. They solve three completely different problems — and picking the wrong one is one of the most common beginner mistakes in web development.
The one-line answer
- Base64 — you need to shove binary or arbitrary data through a channel that only understands plain ASCII text (email, JSON, a data URI).
- URL encoding (percent-encoding) — you need to put a value inside a URL without breaking the URL's structure.
- HTML entities — you need to put user input into an HTML page without the browser reading it as markup.
Everything else in this article is detail. But the detail is where the bugs live, so read on.
Base64: binary data in an ASCII world
Base64 takes any bytes — an image, a gzip stream, a UTF-8 string — and rewrites them using just 64 characters: A–Z, a–z, 0–9, + and /, with = as padding at the end.
The = padding is the classic "is my output broken?" moment: a Base64 string ending in one or two equals signs is not a bug. Three bytes of input become four characters of output, and the padding fills the gap when the input length isn't a multiple of three.
Where you run into it in practice:
- Email attachments — SMTP was designed for 7-bit ASCII, so attachments are Base64-encoded (that's why an emailed 1 MB file becomes roughly 1.37 MB in the raw message).
- JWTs — the header and payload of a JSON Web Token are Base64url-encoded (a URL-safe variant that swaps
+/for-_and drops the padding). - Data URIs —
data:image/png;base64,...embeds an image directly in CSS or HTML.
The cost to remember: Base64 output is about 33% larger than the input. Fine for small payloads, wasteful for big ones. And it is not encryption — anyone can decode it. It's a shipping container, not a safe.
URL encoding: values that must not break the URL
A URL has structure: ? starts the query, & separates parameters, = assigns values, # starts the fragment. If your data contains any of those characters — or a space, or anything outside the ASCII printable range — it must be escaped.
That's what percent-encoding does: each problematic byte becomes a % followed by two hex digits. A space becomes %20, an ampersand becomes %26, and the euro sign — three bytes in UTF-8 — becomes %E2%82%AC.
The classic bug: you build a query string like ?next=/results&lang=en, but the value itself contains an &. If the value isn't encoded, the server reads it as a new parameter. The fix is encoding the value (%26), not the URL. Encoding the whole URL would escape the ? and & that are supposed to be there — that's the opposite bug, and it's just as common.
Rule of thumb: encode values, never the URL itself.
HTML entities: text that must not become markup
When user input lands on an HTML page, five characters have special meaning to the browser: <, >, &, " and '. HTML entity encoding replaces them with <, >, &, " and ', so the browser displays them as text instead of interpreting them as tags or attribute boundaries.
Do this wrong and you don't just get ugly output — you get XSS. A comment containing <script> must be stored or rendered as those six visible characters, never executed.
The sneaky variant: double encoding. If your data is already entity-encoded and something encodes it again, & becomes &amp; and users see a literal & on the page. If you ever see that on a website, that's exactly what happened.
Side by side
| Base64 | URL encoding | HTML entities | |
|---|---|---|---|
| Problem it solves | Binary → ASCII text | Value → safe URL component | Text → safe HTML content |
| Output size | +33% | +200% per escaped char | +4 chars per escaped char |
| Reversible by | Anyone (not encryption) | Anyone | Anyone |
| Typical use | Email, JWT, data URIs | Query strings, path segments | Rendering user input |
| Classic bug | Padding = mistaken for corruption | Encoding the whole URL instead of the value | Double-encoding into &amp; |
What none of them do
None of the three is encryption, and none is compression. If you need secrecy, you need actual encryption with a key. If you need smaller data, you need compression (and then, often, Base64 to ship the compressed bytes as text — which is why the two show up together).