In Python, convert Base64 to hex by decoding it to bytes and calling .hex(): base64.b64decode(value, validate=True).hex().
For canonical standard RFC 4648 Base64, this produces a continuous lowercase hex string with exactly two digits per byte. The important distinction is that Base64 decoding creates bytes; it does not automatically turn those bytes into readable text.

How Python keeps the byte boundaries
Base64 groups three bytes into four characters, while hexadecimal represents each byte with two digits. Python’s base64 module returns a bytes object, and the bytes.hex() method then renders every byte in that fixed-width format. A zero-valued byte remains visible as “00” rather than disappearing or becoming a single “0”.
| Representation | Character set | Byte layout | Strict input format |
|---|---|---|---|
| RFC 4648 Base64 | Uppercase and lowercase letters, digits, plus, and slash | Four characters represent a three-byte group | Canonical padding, length divisible by four, and no whitespace |
| Base16 hexadecimal | Digits 0–9 and letters A–F | Exactly two digits represent each byte | No 0x prefix, separators, whitespace, underscores, or odd trailing digit |
Hex input may use uppercase or lowercase letters, but the browser converter always emits lowercase hex. Standard Base64 output uses the “=” character when the final group contains fewer than three bytes.
Convert Base64 to hex in Python
- Import Python’s built-in module with import base64.
- Store the source as an ASCII string, for example encoded = "TWFu".
- Decode the encoded value with decoded = base64.b64decode(encoded, validate=True).
- Convert the resulting bytes to lowercase hex with hex_value = decoded.hex().
- For strict canonical validation, calculate canonical = base64.b64encode(decoded) == encoded.encode("ascii").
- If the comparison is false, reject the source with raise ValueError("Base64 is not canonical") instead of accepting an alternate spelling.
- Use or print hex_value after the canonical check succeeds.
For the canonical RFC 4648 vector TWFu, the decoded bytes are 4d 61 6e, and the continuous lowercase hex result is 4d616e.
The validate=True option checks that the input belongs to the standard Base64 alphabet, but exact canonicality also depends on the padding and unused pad bits. Re-encoding the decoded bytes and comparing the result with the original input is the strongest simple check: any difference means that the source would not reproduce itself byte for byte after decoding and encoding again.
Do not silently remove whitespace unless the source specification explicitly permits it. Canonical RFC 4648 Base64 has no line wrapping, and changing the input before comparison can hide a malformed or noncanonical value.
Convert hex back to Base64 in Python
The reverse conversion uses base64.b64encode(bytes.fromhex(hex_value)).decode("ascii"). The bytes.fromhex() function interprets the hex string as bytes, and base64.b64encode() returns standard padded Base64 that can be handled as an ASCII string.
Python accepts uppercase and lowercase hex digits, but bytes.fromhex() ignores ASCII whitespace. For a strict workflow that rejects spaces, separators, a 0x prefix, and an odd final nibble, import re and check if len(hex_value) % 2 or not re.fullmatch(r"[0-9A-Fa-f]*", hex_value): raise ValueError("Invalid hex") before calling bytes.fromhex().
Once the hex has passed validation, the output is the same byte sequence represented in canonical Base64. No UTF-8 decoding is involved: if the bytes represent text, decode them separately with UTF-8 only when that interpretation is part of the application.
Run a strict browser conversion
The Base64 to Hex Converter performs the same byte-level operation without requiring Python. It is useful for converting a payload directly in a browser or independently checking a Python result against the same strict profile.
- Choose Base64 to hexadecimal or hexadecimal to Base64, then confirm that the source uses standard RFC 4648 encoding rather than base64url or a MIME container.
- For Base64 to hex, paste canonical padded Base64 without whitespace. For hex to Base64, paste exactly two hex digits per byte with no prefix or separators.
- Run the conversion, then compare the resulting byte length with the expected length and check that leading zero bytes remain represented by “00” in hex.
- Copy the exact converted value. The copy action includes only the result, without labels or explanatory text.
The conversion runs locally in the browser and does not upload the input. It supports payloads up to 500,000 decoded bytes, validates the alphabet and byte boundaries, and rejects malformed padding, noncanonical pad bits, and other invalid input without returning a partial result.
Match the format before converting
- Confirm the Base64 profile. Standard RFC 4648 uses “+” and “/” with required padding. Base64url uses “-” and “_” and often omits padding; it is a different profile and is not accepted by the strict browser converter.
- Distinguish raw Base64 from MIME formatting. Some email and command-line decoders accept line wrapping or missing padding. Those are separate parsing profiles, not canonical RFC 4648 input.
- Keep hex pairs explicit. A value such as “0f” represents one byte, while a lone “f” is incomplete. The converter rejects “0x”, spaces, separators, underscores, and an odd number of hex digits.
- Remove surrounding containers separately. Data-URL prefixes, MIME headers, line wrapping, and checksum fields are not part of either encoding. Decode or validate those surrounding structures independently.
- Check canonical spelling. A successful decode alone does not prove that a Base64 source has the required padding and zero pad bits. Re-encoding the decoded bytes provides that check in Python.
The formal alphabet, padding, and byte-grouping rules are defined in RFC 4648. For security-sensitive protocols or signed fixtures, compare the final representation against the governing specification or an official test vector rather than treating a successful conversion as semantic validation.
Preserve the bytes after conversion
- Check leading zeros. Each byte must remain two hex digits wide, so a leading zero byte appears as “00”.
- Keep the representation layer separate from the data layer. Neither the Python functions nor the browser converter interprets the bytes as UTF-8, JSON, certificates, images, keys, checksums, or files.
- Do not treat encoding as protection. Base64 and hex are reversible representations of the same bytes; they provide no encryption, hashing, signing, authentication, or access control.
- Handle sensitive values carefully. Even local conversion can expose credentials, tokens, private keys, or personal records through clipboards, logs, screenshots, issue trackers, analytics fields, and shared browser sessions.
A correct conversion changes only the representation. The byte count, byte order, and leading zero bytes remain the same, so those properties should be verified whenever the result will be used in a protocol field, test fixture, cache key, or serialized value.
Related reading: Hex to Text Converter Alternative: How to Choose One.
Related reading: File to Base64 Converter Alternative: What to Compare.