Guides

Base64 vs Hex vs Binary: When to Use Each

Priya Nairยทยท7 min read

I ran a small experiment to make a point to a teammate. I took the same 1 KB chunk of binary data and wrote it out three ways. Base64 produced about 1.37 KB. Hex produced 2 KB. Written as literal 1s and 0s, it ballooned to 8 KB. Identical data, three wildly different sizes โ€” and that size gap is basically the whole reason you'd choose one representation over another.

All three answer the same question โ€” how do I write these bytes using printable characters? โ€” and the understanding Base64 encoding piece covers one of them in depth. Here's how to pick between all three.

The units-of-measurement analogy

Base64, hex, and binary are like miles, kilometres, and feet. They all describe the same distance; they just use different-sized units, so the number you write down changes. Feet give you fine-grained detail and a huge number; miles give you a compact number and less detail. None is 'more correct' โ€” you pick the unit that fits the situation. Same bytes underneath, different notation on the page.

Binary: one bit at a time

Binary writes each byte as its eight bits โ€” 01001000 for the letter H. It's the ground truth of how computers store everything, and reading it is a genuinely useful skill for understanding flags, masks, and encodings (our guide on how to read binary shows the patterns). But as transport it's hopeless: eight characters per byte means eight times the size. You use binary to learn and to inspect individual bits, not to move data.

Hex: the readable middle

Hexadecimal packs four bits into one character, so a byte is exactly two hex digits โ€” 48 for that same H. That clean two-digits-per-byte fit (the reason hex exists, covered in what is hexadecimal) makes it the format of choice whenever a human needs to read bytes:

  • Cryptographic hashes (a SHA-256 is 64 hex characters).
  • Colors in CSS (#48c0a0).
  • Memory dumps and byte-level debugging.

Hex doubles the size versus raw bytes, but you can scan it, align it, and spot patterns. Convert either direction with the hex to text and text to binary tools.

Base64: the compact courier

Base64 packs six bits into each character, bundling three bytes into four characters. That's why it's the smallest of the three, adding only ~33% overhead. It's not built for reading โ€” it's built for survival: getting binary safely through channels that only accept text, like JSON, email, or a data: URI. Encode and decode with the Base64 Encoder and Base64 Decoder.

Side by side

Binary Hex Base64
Bits per character 1 4 6
Size vs raw bytes 8ร— 2ร— ~1.33ร—
Human-readable Hard Easy No
Typical use Learning, bit flags Hashes, colors, debugging Transport in text channels
'H' looks like 01001000 48 SA==

Which one, when

  • Moving binary through a text-only pipe (JSON, email, data URI) โ†’ Base64. Smallest, purpose-built.
  • Reading or comparing bytes by eye (a hash, a color, a hex dump) โ†’ Hex. The readable middle ground. To turn readable text into raw bytes for inspection, the text to binary and binary to text tools show the bits directly.
  • Understanding what individual bits do (flags, masks, teaching) โ†’ Binary. Ground truth, terrible for transport.

The verdict

They're three units for the same measurement, so 'which is best' is the wrong question โ€” 'best for what?' is the right one. Base64 to transport, hex to read, binary to understand. Keep that mapping and you'll never again reach for the bloated 8ร— representation when you just needed to move a file, or squint at unreadable Base64 when you actually needed to compare two bytes.

Try the tools

Frequently Asked Questions

What's the difference between Base64, hex, and binary?

All three represent the same underlying bytes using printable characters, but at different sizes. Binary uses 1 bit per character (8ร— the size), hex uses 4 bits (2ร—), and Base64 uses 6 bits (~1.33ร—). Base64 is most compact, hex is most readable.

Why is Base64 smaller than hex?

Base64 encodes 6 bits per character versus hex's 4, so it packs more data into each character. Three bytes become four Base64 characters but six hex characters, making Base64 roughly 33% overhead against hex's 100%.

Why are hashes shown in hex, not Base64?

Hex's fixed two-characters-per-byte makes hashes easy to align, compare, and read by eye. Some systems do use Base64 for hashes to save space, but hex remains the convention for readability and byte-level comparison.

Is hex or Base64 better for storing binary in a database?

Base64 if space matters, since it's more compact. Hex if you need to read or query the value by eye. Many databases also offer a native binary/blob type, which avoids text encoding overhead entirely.

Can I convert between Base64, hex, and binary?

Yes โ€” they're just different notations for the same bytes, so conversion is lossless. Decode one back to raw bytes and re-encode in another format using a Base64 or hex tool; nothing is lost in the round trip.

PN

Priya Nair writes for CodeUtilityKit, where the team builds free, privacy-first developer tools that run entirely in your browser. Every guide is written and reviewed by developers who use these tools daily.