Should you encode binary data as Base64 or hex?

By TechCompare · Updated · How we check

Use Base64 for data interchange, APIs, and anywhere size matters. Use hex for hashes, debugging, and anywhere readability matters. The two cover different jobs, so it helps to be comfortable with both.

Base64 and hexadecimal both encode binary data as text, but they have different purposes. The choice comes down to whether you prioritize compactness or readability.

Encoding focus
Base64 vs Hex
base64-vs-hex
Category
Format Comparison
Comparing encoding schemes side by side

How this is calculated

Base64 is the standard for embedding binary data in JSON, email attachments (MIME), data URIs in CSS, and JWT tokens. Hex is preferred for cryptographic hashes (SHA-256 fingerprints), memory dumps, color codes, and anywhere you want to see individual byte values at a glance.

For debugging and inspection, hex wins because each pair of characters is exactly one byte.

Verdict

Use Base64 when size matters and hex when a person needs to read the bytes. Base64 expands data by 33% against 100% for hex, so a SHA-256 hash shrinks from 64 hex characters to 44 in Base64, which is why JWT and MIME lean on it. Hex stays relevant wherever two characters per byte makes the value readable at a glance.

More Encoding scenarios

UTF-8 vs ASCII
ASCII maps 128 English characters to 7-bit values.
View details
URL encoding guide
Percent-encoding (also called URL encoding) represents a byte with a percent sign followed by two hex digits when that byte can't appear literally in the URL component being built.
View details
Binary-to-text encodings
Binary-to-text encodings convert arbitrary bytes into printable characters so they can travel through text-only channels: JSON, XML, email, URLs, and printed QR codes.
View details

Frequently asked questions

What's the difference between Base64 and hex encoding?
Base64 packs 6 bits per character using a 64-character alphabet, so it expands data by about 33%. Hex packs 4 bits per character using a 16-character alphabet and expands data by exactly 100%. Both turn binary into text, but Base64 is built for compactness and hex is built for readability.
When should I use hex instead of Base64?
Whenever a human needs to read individual bytes: cryptographic hashes, memory dumps, TLS fingerprints, and color codes. Each pair of hex characters is exactly one byte, which makes values like a SHA-256 hash scannable in a way 44 characters of Base64 never are. Hex is also what every debugger and hex editor speaks natively.
How much bigger is hex than Base64?
For large payloads (megabytes of binary), hex is about 1.5 times as long as Base64, not three times. A SHA-256 hash is 64 characters in hex and 44 in Base64. Hex uses 4 bits per character, while Base64 uses 6, so their raw expansions are 100% and about 33%.