Blog · Encoding
Base64 to Hex: Complete Conversion Guide
Convert Base64 to hexadecimal and back. Learn when to use hex instead of Base64, and how to convert between the two formats in JavaScript, Python, and online.
Base64 is great for text-safe transmission. But sometimes you need hexadecimal instead. Hex is easier to read, debug, and compare. Every byte becomes exactly two characters. No padding, no special characters. Here is how to convert between the two.
Why Convert Base64 to Hex?
Hex makes binary data human-readable at a glance:
- Debugging hash values. SHA256, MD5, and other hashes are typically displayed in hex. If you get a Base64-encoded hash from an API, convert it to hex to compare.
- Inspecting binary payloads. Each pair of hex digits is one byte. Easy to count offsets and spot patterns.
- Firmware and embedded systems. Many tools output hex dumps. Base64 is rare in low-level contexts.
JavaScript: Base64 to Hex
function base64ToHex(base64) {
// Decode Base64 to raw bytes
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
// Convert each byte to 2-char hex
return [...bytes].map(b => b.toString(16).padStart(2, '0')).join('');
}
base64ToHex("SGVsbG8="); // "48656c6c6f" (Hello)
// Decode Base64 to raw bytes
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
// Convert each byte to 2-char hex
return [...bytes].map(b => b.toString(16).padStart(2, '0')).join('');
}
base64ToHex("SGVsbG8="); // "48656c6c6f" (Hello)
JavaScript: Hex to Base64
function hexToBase64(hex) {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
}
return btoa(String.fromCharCode(...bytes));
}
hexToBase64("48656c6c6f"); // "SGVsbG8="
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
}
return btoa(String.fromCharCode(...bytes));
}
hexToBase64("48656c6c6f"); // "SGVsbG8="
Python: One-Liner
import base64, binascii
# Base64 to Hex
decoded = base64.b64decode("SGVsbG8=")
hex_str = binascii.hexlify(decoded).decode()
# "48656c6c6f"
# Hex to Base64
raw = bytes.fromhex("48656c6c6f")
b64 = base64.b64encode(raw).decode()
# "SGVsbG8="
# Base64 to Hex
decoded = base64.b64decode("SGVsbG8=")
hex_str = binascii.hexlify(decoded).decode()
# "48656c6c6f"
# Hex to Base64
raw = bytes.fromhex("48656c6c6f")
b64 = base64.b64encode(raw).decode()
# "SGVsbG8="
Base64 vs Hex: When to Use Which
| Feature | Base64 | Hex |
|---|---|---|
| Size increase | 33% larger | 100% larger |
| Characters | A-Z, a-z, 0-9, +, / | 0-9, A-F |
| Human readable | No | Partially (byte-pairs) |
| Best for | Network transfer, JSON, data URIs | Hashes, debugging, firmware |
Need to encode or decode Base64?
Try our free online tool. Paste Base64 or hex and get instant results.
Open Base64 Tool