TechCompare LogoTechCompare

Base64 vs hexadecimal encoding: which should you use for binary data?

Use Base64 for data interchange, APIs, and anywhere size matters. Use hex for hashes, debugging, and anywhere readability matters. They're not competitors. They're different tools for different jobs, and any competent developer should be comfortable with both.

Base64 and hexadecimal both encode binary data as text, but they serve different purposes. Base64 packs 6 bits per character (64-character alphabet) and expands data by about 33%. Hex packs 4 bits per character (16-character alphabet) and expands data by exactly 100%. The choice comes down to whether you prioritize compactness or readability.

By TechCompare · Updated

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. A SHA-256 hash in hex is 64 characters, while the same hash in Base64 is 44 characters. For large payloads (megabytes of binary), Base64's 33% overhead saves significant bandwidth and storage compared to hex. For debugging and inspection, hex wins because each pair of characters is exactly one byte.

Verdict

The overhead gap is real and measurable: Base64 packs 6 bits per character for 33% expansion, hex packs 4 bits for 100% expansion. A SHA-256 hash shrinks from 64 hex characters down to 44 in Base64, which is why JWT and MIME lean on it. Hex stays relevant for hashes, memory dumps, and color codes where 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) replaces characters that aren't safe in a URL with a percent sign followed by two hex digits.
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 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?
About three times the character count for the same bytes. A SHA-256 hash is 64 characters in hex and 44 in Base64, and the gap widens with payload size. For megabytes of binary in a JSON payload or email attachment, Base64's 33% overhead versus hex's 100% is the difference between a bandwidth bill you notice and one you don't.