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

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:

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 &lt;, &gt;, &amp;, &quot; and &#39;, 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, &amp; becomes &amp;amp; and users see a literal &amp; on the page. If you ever see that on a website, that's exactly what happened.

Side by side

Base64URL encodingHTML entities
Problem it solvesBinary → ASCII textValue → safe URL componentText → safe HTML content
Output size+33%+200% per escaped char+4 chars per escaped char
Reversible byAnyone (not encryption)AnyoneAnyone
Typical useEmail, JWT, data URIsQuery strings, path segmentsRendering user input
Classic bugPadding = mistaken for corruptionEncoding the whole URL instead of the valueDouble-encoding into &amp;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).