Convert Base64 to hexadecimal
Convert Base64 to hex by decoding it to bytes and writing each byte as two hex digits. Includes commands, code, input pitfalls and sensitive-data advice.
To convert Base64 to hex, decode the Base64 string into its raw bytes, then write each byte as two hexadecimal digits. For example, SGVsbG8= decodes to the five bytes of the word Hello, which in hex are 48656c6c6f. Both formats describe exactly the same bytes, so the conversion loses nothing and can be reversed.
What the conversion does and does not do
Base64 uses an alphabet of 64 characters. Each character carries 6 bits, so four characters hold three bytes, and the = sign pads the end when the data length is not a multiple of three. Hexadecimal uses 16 characters (0–9 and a–f). Each character carries 4 bits, so every byte becomes exactly two characters. Because hex spends 2 characters per byte and Base64 spends 4 characters per 3 bytes, the hex version of the same data is about one and a half times as long as the Base64 version.
This is a change in how data is represented, not a visual text effect. Unicode character substitution swaps letters for look-alike symbols, CSS styling changes only how text appears on screen, and bidirectional controls change display direction. Base64-to-hex conversion does none of these: the output is plain ASCII. It also preserves byte order. It does not reverse or mirror anything, so if the destination expects bytes in reverse order (for example, a little-endian number), that is a separate step.
Worked example
The string AQID shows the mechanics. Each Base64 character is looked up in the alphabet, converted to 6 bits, regrouped into 8-bit bytes, and then written as hex.
Base64: A Q I D
Index: 0 16 8 3
Bits: 000000 010000 001000 000011
Regroup: 00000001 00000010 00000011
Hex: 01 02 03
Result: 010203
Converting on the command line
On Linux and similar systems, pipe the decoded bytes into a hex dump tool. Plain xxd output wraps long lines, so the final tr command joins them into one string. The od version needs the -v option; without it, od collapses repeated lines and the output would be wrong for data with repeated runs of bytes. In some base64 implementations the decode option is not -d, so check the local help text if that option is rejected.
echo -n 'SGVsbG8=' | base64 -d | xxd -p | tr -d '\n'
# 48656c6c6f
echo -n 'SGVsbG8=' | base64 -d | od -An -v -tx1 | tr -d ' \n'
# 48656c6c6f
Converting in Python
Decode to bytes and call .hex() directly on the result. Pass validate=True for standard Base64. By default, Python's b64decode silently discards characters that are not in the alphabet, which can hide damaged input. For the URL-safe alphabet, restore any missing padding before decoding. In Python 3.8 and later, .hex() also accepts a separator for grouped output.
import base64
s = "SGVsbG8="
data = base64.b64decode(s, validate=True)
print(data.hex()) # 48656c6c6f
print(data.hex(" ")) # 48 65 6c 6c 6f
# URL-safe input, padding stripped
u = "-_8"
u += "=" * (-len(u) % 4)
print(base64.urlsafe_b64decode(u).hex()) # fbff
Converting in JavaScript
In Node.js, Buffer converts between the two encodings in one step. According to Node's documentation, its base64 decoder also accepts the URL-safe alphabet. It is lenient in general, so validate the input separately if malformed data must be rejected. In browsers, atob returns a string of byte values that you then format yourself. atob rejects the URL-safe characters - and _, so translate them to + and / first.
// Node.js
Buffer.from("SGVsbG8=", "base64").toString("hex"); // '48656c6c6f'
// Browser
const bytes = Uint8Array.from(atob("SGVsbG8="), c => c.charCodeAt(0));
const hex = Array.from(bytes, b => b.toString(16).padStart(2, "0")).join("");
// '48656c6c6f'
Input problems to check before converting
- Line breaks and spaces: Base64 copied from email, certificates or config files is often wrapped at a fixed width. Remove the whitespace before using a strict decoder.
- URL-safe alphabet: this variant uses - and _ instead of + and / and often omits padding. Use a URL-safe decoder, or translate the characters and add = until the length is a multiple of four.
- Plus signs turned into spaces: if the value travelled through a URL query string or form encoding, a + may have become a space. Change spaces back to + before decoding.
- Silently skipped characters: lenient decoders may drop invalid characters instead of failing, so the hex output can look plausible while being wrong. Use strict validation when correctness matters.
- Accidental text decoding: go straight from bytes to hex. Converting the bytes to a text string first, for example as UTF-8, can replace invalid sequences and change the data before it reaches hex.
Will the hex output survive copy and paste?
Hex output contains only digits and the letters a–f, all basic ASCII. It therefore avoids the +, / and = characters that URLs and some form handlers treat specially. When problems occur, they usually involve formatting rather than lost characters. Long unbroken strings may be wrapped or split when pasted into documents, chat messages or email, and the destination may expect a particular layout. Check these details against what the receiving system requires:
- Letter case: 4F and 4f are the same value, but a plain string comparison treats them as different.
- Separators: some tools expect no separators, while others expect spaces or colons between bytes.
- Prefix: some contexts require 0x, either once at the start or before each byte.
- Line length: a few formats require fixed-width lines, while others reject any line breaks.
Long hex strings are hard for people to read and check. Splitting the output into groups of bytes makes visual comparison easier. If the destination needs the ungrouped form, keep a grouped copy for people to check and paste the ungrouped version. Screen readers may announce an unbroken hex run as one unpronounceable word or read it character by character, depending on the software and its settings. This has not been tested here, so check with the assistive technology your readers actually use. If people need to read the content rather than inspect the bytes, decoded text is a better output than hex.
Sensitive data and online converters
Base64 and hex are encodings, not encryption. Anyone can reverse them. Base64 values often contain access tokens, keys or certificates, and pasting them into a web converter may send them to a server you do not control. You can only confirm that a given online tool processes data locally from its own documentation or by inspecting its network activity. This article does not evaluate specific tools. For anything confidential, use the local commands or code above. Read the value from a file instead of typing it into an echo command, so the secret does not end up in shell history:
base64 -d < token.b64 | xxd -p | tr -d '\n'
When hex is not the right target
Hex suits inspecting raw bytes, comparing hashes or checksums, debugging binary formats, and systems that require hex input. If the Base64 contains readable text, decoding it to text is more useful. If the destination accepts Base64, keeping it is more compact.