Base64, URL and HTML Encoding, Explained

After reading this you will know what Base64, URL percent-encoding and HTML entities each do to your text, why the byte counts change, and which one to reach for in a given spot.

What these encodings are

All three tools on this page solve the same class of problem: you have some text or binary data, and you need to move it through a channel that only accepts a restricted set of characters. Each encoding maps your original bytes into a "safe" subset, and each has an exact inverse that gives the bytes back.

Here is one hook. The word Hi! is three bytes: 0x48 0x69 0x21. Base64 turns it into SGkh, four printable ASCII characters that survive email, JSON and copy-paste without damage. URL percent-encoding leaves Hi alone but turns the space in Hi there into Hi%20there. HTML entity encoding turns <b> into &lt;b&gt; so a browser prints the tag instead of running it.

None of these is encryption. They are reversible mappings with no secret. That distinction drives most of the mistakes people make with them.

When to use each one

Pick the encoding by the channel that will carry the data, not by habit.

Base64
Use it when a text-only channel must carry arbitrary bytes: a data URI for a small image, a binary blob inside JSON, or a certificate in PEM form. It expands size by about 33 percent, so it is a poor choice for large files.
URL percent-encoding
Use it when a value goes into a URL. The component variant is for a single field such as a query value or a path segment. The full-URL variant preserves the structural characters (:, /, ?, #) so it does not break a whole address.
HTML entities
Use it when user text lands inside an HTML page and must not be parsed as markup. Encoding <, >, & and " is the baseline defense against many injection bugs.

Do not use Base64 to hide passwords, tokens or API keys. Anyone with the string can decode it in one step. If you need secrecy, encrypt. If you need integrity, sign. See the HMAC tool below.

The Base64 math

Base64 reads your bytes in groups of three (24 bits) and rewrites them as four characters of 6 bits each. Six bits have 64 possible values, which is where the name comes from. The alphabet is A-Z, a-z, 0-9, then + and /, defined in RFC 4648.

\text{output bytes} = 4 \cdot \left\lceil \frac{n}{3} \right\rceil

Here n is the number of input bytes. The ceiling accounts for padding: when n is not a multiple of 3, the last group is padded with = characters so the output length stays a multiple of 4. For n = 3 you get 4 characters and no padding. For n = 1 you get 2 real characters plus ==. For n = 2 you get 3 real characters plus =.

The overhead is 4/3 \approx 1.333, so encoding always adds roughly one third. A 900-byte input becomes 1200 Base64 characters exactly, since 900 divides by 3.

The URL-safe variant (also RFC 4648) swaps + for - and / for _, because + and / have meaning inside URLs. It often drops the = padding too. This is the form you see in JWTs.

The staircase: output jumps by 4 characters every 3 input bytes, so the length is always a multiple of 4.

A worked example you can reproduce

Encoding "Hello, World!" three ways

Start with the 13-byte string Hello, World!. The comma, space and exclamation mark make it interesting for all three encoders.

  1. Base64. Thirteen bytes is not a multiple of 3. Apply the formula: 4 \cdot \lceil 13/3 \rceil = 4 \cdot 5 = 20 characters. The result is SGVsbG8sIFdvcmxkIQ==. The trailing == tells you the last group held a single byte (the !).
  2. URL component. Letters stay as themselves. The comma becomes %2C, the space becomes %20, the exclamation mark becomes %21. You get Hello%2C%20World%21.
  3. HTML entities. None of these characters are HTML-special, so a minimal encoder leaves the string unchanged: Hello, World!. Add a < and it would become &lt;.

Decode any of these and you recover the exact 13 bytes. That round trip is the whole point.

Base64 output length follows a staircase: 1, 2 or 3 input bytes all produce 4 output characters; 4 to 6 bytes produce 8; and so on. The formula is 4 times the ceiling of n divided by 3. Padding fills the gap: 1 leftover byte adds "==", 2 leftover bytes add "=".

Reading the output correctly

The shape of the output tells you what happened. In Base64, count the trailing = signs: zero means the input length was a multiple of 3, one means two extra bytes, two means one extra byte. If you see - or _, you are looking at the URL-safe alphabet, and a standard decoder may reject it.

In percent-encoding, every escape is a % followed by exactly two hex digits. %20 is a space, %2F is a slash, %3F is a question mark. A lone % not followed by two hex digits is malformed and most decoders will throw.

UTF-8 matters here. The character é is two bytes in UTF-8 (0xC3 0xA9), so it percent-encodes to %C3%A9, not a single escape. The euro sign € is three bytes and becomes %E2%82%AC. If you want to see the byte count behind a character before you encode, the string length inspector below breaks it down.

Same characters, three encodings
InputBase64URL componentHTML
AQQ==AA
<PA==%3C&lt;
spaceIA==%20space
éw6k=%C3%A9&eacute;
€4oKs%E2%82%AC&euro;

Common mistakes

The errors below cause real bugs in production. Each has a simple check.

  • Mixing Base64 alphabets. Feeding a URL-safe string with - and _ into a standard decoder fails or corrupts. Match the alphabet on both ends.
  • Encoding a whole URL with the component function. That turns https://x.com/?q=1 into a mangled string where the :// and / are escaped. Use the component variant only for individual field values.
  • Double-encoding. Percent-encoding an already-encoded value turns %20 into %2520, because the % itself becomes %25. If you see %25 where you expected a space, you encoded twice.
  • Trusting Base64 for privacy. It is a reversible transform with no key. Treat it as a text wrapper, never as a lock.
  • Forgetting the byte count grows. A 5 MB file becomes about 6.67 MB in Base64. Check the byte size converter below if a payload limit is close.

When a value must be both URL-safe and byte-safe (a token in a query string), Base64-encode first with the URL-safe alphabet, then you usually need no percent-encoding at all, since A-Z, a-z, 0-9, - and _ are all URL-safe.

Related tools

These pages pick up where this one stops. A JWT is three URL-safe Base64 segments joined by dots, so the JWT decoder is the natural next step after you decode one by hand. To check the byte or grapheme count behind a tricky UTF-8 string before encoding, use the string length inspector. To reason about the 33 percent Base64 overhead against a size limit, the byte size converter converts between KB, KiB, MB and MiB. When you actually need integrity or secrecy rather than a text wrapper, reach for the hash generator or the HMAC calculator.

Frequently asked questions

Why does my Base64 string end in one or two equals signs?

The = is padding. Base64 works in blocks of 4 characters representing 3 bytes. If your input length is not a multiple of 3, the last block is short and gets padded. One extra input byte yields ==, two extra bytes yield =, and an exact multiple of 3 yields none.

Is Base64 secure?

No. It has no key and anyone can decode it instantly. It only makes bytes safe to transport through text channels. For secrecy encrypt, for tamper-detection use an HMAC or a signature.

What is the difference between encodeURI and encodeURIComponent?

The component version escapes structural characters like /, ?, & and =, so it suits a single query value. The full-URL version leaves those alone so a complete address stays valid. Use the component form for a value, the full form for a whole URL.

Why does one accented character become several percent escapes?

Percent-encoding operates on UTF-8 bytes, not characters. é is two bytes, so it becomes %C3%A9. A three-byte character like € becomes %E2%82%AC.

How much larger will my data get after Base64?

About 33 percent, exactly 4/3 before padding. A 3000-byte input becomes 4000 characters. A 5 MB file becomes roughly 6.67 MB.