Base64 vs Hex Encoding
Both Base64 and hexadecimal (hex) encode binary data as text — but they make different tradeoffs between size efficiency and human readability. Here's when to use each.
How They Work
Hex (Base16) encodes each byte as two hexadecimal digits (0-9, a-f). Each hex digit represents 4 bits, so 1 byte = 2 characters.
Base64 groups 3 bytes and encodes them as 4 characters. Each character represents 6 bits.
Input: 3 bytes = [0x48, 0x65, 0x6C] = "Hel" Hex: "48656c" → 6 characters (200% of original) Base64: "SGVs" → 4 characters (133% of original)
Size Comparison
Encoding Characters Overhead Bits per char ───────────────────────────────────────────── Binary — 0% 8 bits Hex 16 100% 4 bits Base64 64 ~33% 6 bits Base85 85 ~25% 6.5 bits
Encoding the same 1 KB of binary data:
Original: 1,024 bytes Hex: 2,048 bytes (+100%) Base64: 1,368 bytes (+33.6%)
Base64 is significantly more space-efficient than hex. For large files (images, PDFs), this difference matters.
Alphabet Comparison
Hex alphabet (16 chars): 0123456789abcdef Base64 alphabet (64 chars): ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
Hex is more human-readable — each byte maps to exactly two characters, and you can easily read byte values. Base64 is more compact but harder to read byte-by-byte.
When to Use Hex
- Cryptographic hashes: MD5, SHA-1, SHA-256 are almost always displayed as hex (
sha256: a1b2c3d4...) - Color values in CSS:
#ff6b6b,#0891b2 - Memory addresses and byte dumps: Debugger output, hex editors
- MAC addresses and UUIDs:
00:1A:2B:3C:4D:5E,550e8400-e29b-41d4-a716-446655440000 - Database-stored binary: PostgreSQL
byteacolumns often display as hex - When byte-level inspection matters: Hex lets you see exactly what each byte is
When to Use Base64
- File and image transfer: Email attachments, data URIs, API responses with binary fields
- JWT tokens: JWTs use Base64URL for compact encoded JSON payloads
- TLS/SSL certificates (PEM format): The Base64-encoded DER data between BEGIN/END markers
- HTTP Basic Auth:
Authorization: Basic dXNlcjpwYXNzd29yZA== - SSH public keys:
ssh-rsa AAAA... - Anywhere size matters: Base64's 33% overhead is much better than hex's 100%
Code Examples
// JavaScript: same data, both encodings
const data = crypto.getRandomValues(new Uint8Array(16)); // 16 random bytes
// Hex
const hex = Array.from(data).map(b => b.toString(16).padStart(2, '0')).join('');
// e.g., "a1b2c3d4e5f6789012345678abcdef01" (32 chars)
// Base64
const base64 = btoa(String.fromCharCode(...data));
// e.g., "oQ+TpNRn..." (24 chars + padding)
## Python: hash as hex vs base64 import hashlib, base64 data = b"hello world" digest = hashlib.sha256(data).digest() # 32 bytes hex_str = digest.hex() # 64 hex chars base64_str = base64.b64encode(digest).decode() # 44 base64 chars print(hex_str[:32] + "...") # b94d27b9934d3e08a52e52d7da7dabfac484efe04294e6... print(base64_str) # uU0nusNkPkv5ZNnCxeUKWA==
Quick Reference
Use case Format Why ───────────────────────────────────────────────────────────── SHA-256 hash display Hex Convention, readable JWT token Base64URL Compact, URL-safe PEM certificate Base64 Standard (RFC 7468) Data URI (CSS/HTML image) Base64 ~33% overhead vs 100% for hex UUID / GUID Hex Convention SSH key Base64 Convention HTTP Basic Auth Base64 RFC 7617 Password hash storage Hex Convention (bcrypt uses Base64) PostgreSQL bytea Hex Default display format JSON binary field Base64 Compact, JSON-friendly
Encode or decode Base64 instantly
Paste any text or binary data into base64.dev to encode or decode it — no sign-up needed.
Open base64.dev →