Hex to Base64 for digests and binary data
Hex and Base64 are two text representations of the same bytes. Hex uses two characters per byte; Base64 uses about 1.33, so it is shorter. The hex to Base64 converter parses the hex into raw bytes and re-encodes them — it does not encode the hex characters themselves, which is the mistake that produces wrong results in many ad-hoc scripts.
Example
Input:
48 65 6c 6c 6f 20 f0 9f 91 8bOutput:
SGVsbG8g8J+Riw==The ten bytes spell “Hello 👋” in UTF-8. Encoding the hex text itself would instead produce a much longer, meaningless string.
Why convert hash digests?
Many tools print hashes in hex, but several standards want Base64 of the raw digest bytes:
- Subresource Integrity (SRI). The
integrityattribute on<script>and<link>tags issha384-followed by Base64 of the SHA-384 digest. Generate the hex digest with the SHA-384 Hash Generator, then convert it here. - HTTP checksum headers.
Content-MD5and several cloud storage checksum headers carry Base64 digests. - Keys and certificates often appear in hex in one tool and are needed in Base64 by another (for example in JSON Web Keys, which use Base64URL).
Accepted hex formats
Separators (spaces, commas, colons, semicolons, hyphens), 0x and \x prefixes and mixed case are removed before parsing, so plain hex from xxd -p, OpenSSL (AB:CD:EF), the values of a C array (without the braces) or hash tools can be pasted directly. A default xxd dump also has offset and text columns, which must be removed first. An odd number of digits gets a leading zero; any non-hex character is reported as an error.
The length of the hex input tells you what you have: two hex characters per byte, so 32 characters is 16 bytes (MD5), 64 characters is 32 bytes (SHA-256) and so on.
Digest lengths at a glance
| Algorithm | Hex length | Base64 length |
|---|---|---|
| MD5 | 32 | 24 (ends in ==) |
| SHA-1 | 40 | 28 (ends in =) |
| SHA-256 | 64 | 44 (ends in =) |
| SHA-384 | 96 | 64 (no padding) |
| SHA-512 | 128 | 88 (ends in ==) |
Doing it on the command line
echo -n "48656c6c6f" | xxd -r -p | base64 converts hex to Base64 in a shell, and in Python base64.b64encode(bytes.fromhex(h)) does the same. Choose URL-safe output for JWKs, JWT signatures and other Base64URL contexts.
Convert back with Base64 to Hex. Everything runs locally in your browser.