Encoding · Guide
Base64 vs Hex — Key Differences
Both Base64 and hex are ways to represent binary data as text — but they have very different characteristics. Here's when to use each and why it matters.
Base64 Encoder / Decoder
Encode and decode Base64 strings instantly. Free and private — runs in your browser.
Open Base64 Encoder / Decoder →Quick comparison
| Feature | Base64 | Hex (Base16) |
|---|---|---|
| Characters used | A-Z, a-z, 0-9, +, / (64 chars) | 0-9, a-f (16 chars) |
| Size vs binary | +33% larger | +100% larger (2x) |
| Readability | Compact but opaque | Longer but byte-level readable |
| URL safe? | No (use Base64URL variant) | Yes |
| Common use | JWTs, data URLs, email | Hashes, checksums, colour codes |
When to use Base64
Base64 is the right choice when you need to embed binary data in text-based formats — especially when file size matters:
- JWTs — header and payload are Base64URL encoded
- Data URLs —
data:image/png;base64,...for inline images - Email attachments — MIME encoding
- API authentication — HTTP Basic Auth is Base64-encoded
- Embedding files in JSON — smaller than hex for the same data
When to use hex
Hex is better when human readability at the byte level matters, or when debugging binary data:
- Cryptographic hashes — SHA-256, MD5 output is shown as hex (e.g.
a3f5...) - Checksums — file integrity verification
- Colour codes — CSS hex colours (
#ff6600) - Memory addresses — debuggers and hex editors show addresses in hex
- Binary protocol debugging — each hex pair = exactly one byte
Size comparison example
Same data, three encodings
Binary data: [0x48, 0x65, 0x6C, 0x6C, 0x6F] (5 bytes = "Hello") Hex: 48656c6c6f (10 chars — 2x the binary size) Base64: SGVsbG8= (8 chars — 1.6x the binary size) Text: Hello (5 chars — original)
For a 1 MB binary file: hex produces 2 MB of text, Base64 produces 1.33 MB. Base64 is significantly more compact when transmitting or storing binary data as text.
Converting between formats in JavaScript
Hex to Base64
function hexToBase64(hex) {
const bytes = hex.match(/.{1,2}/g).map(b => parseInt(b, 16));
return btoa(String.fromCharCode(...bytes));
}
hexToBase64('48656c6c6f'); // "SGVsbG8="Base64 to Hex
function base64ToHex(b64) {
return [...atob(b64)]
.map(c => c.charCodeAt(0).toString(16).padStart(2, '0'))
.join('');
}
base64ToHex('SGVsbG8='); // "48656c6c6f"Frequently asked questions
Is Base64 more secure than hex?
Neither is secure — both are encodings, not encryption. Anyone can decode both without a key. The choice between them is purely about format and size, not security.
Why do SHA-256 hashes look like hex strings?
Cryptographic hash functions output raw bytes. Hex is the conventional way to display hashes because each hex character directly represents 4 bits — making it easy to see exactly how many bytes the hash is (SHA-256 = 32 bytes = 64 hex chars).
Try it free — no sign-up needed
Runs entirely in your browser. Nothing uploaded, nothing stored.
Open Base64 Encoder / Decoder →