Skip to content

Hex to Base64 Converter

Paste hex and get Base64 instantly. Works for hashes, checksums and any hex bytes, written with or without spaces, colons or 0x in front. Switch direction to decode Base64 to hex.

6 hex digits = 3 bytes = 4 Base64 characters

Base64
SGVsbG8=

5 bytes read from spaced hex → 8 characters

Everything runs in your browser; nothing is uploaded.

How to Convert Hex to Base64

Hex and Base64 are two ways of writing the same bytes as text. Hex uses 2 characters per byte. Base64 uses 4 characters for every 3 bytes, so it is a third shorter. You don't convert the letters directly: you turn the hex back into bytes, then write those bytes in Base64.

  1. Read the hex two digits at a time. Each pair is one byte (00 to FF).
  2. Take the bytes three at a time and write them out as 24 bits.
  3. Cut the 24 bits into four groups of 6. Each group is a number from 0 to 63.
  4. Look each number up: A–Z are 0–25, a–z are 26–51, 0–9 are 52–61, then + is 62 and / is 63.

If one or two bytes are left over at the end, fill the missing bits with zeros and add an = for each missing character. You can paste hex with spaces, colons, line breaks or 0x in front; the tool ignores them.

every 6 hex digits = 3 bytes = 4 Base64 characters

Worked Example: 48 65 6C 6C 6F to SGVsbG8=

The hex 48 65 6C 6C 6F is the word "Hello". Five bytes make one full group of three, with two bytes left over.

Step 1: Write the first three bytes, 48 65 6C, as bits

01001000 01100101 01101100

Step 2: Regroup into sixes and look each one up

010010 = 18 = S · 000110 = 6 = G · 010101 = 21 = V · 101100 = 44 = s → SGVs

Step 3: Do the last two bytes, 6C 6F, filling with zeros

011011 = 27 = b · 000110 = 6 = G · 111100 = 60 = 8 · one missing → = → bG8=

Result: SGVsbG8=, 8 characters for 5 bytes. Paste 48 65 6C 6C 6F into the tool to check it.

Hex to Base64 Chart

The = signs follow the byte count: 1 byte left over ends in ==, 2 bytes in =, and a full group of 3 in nothing.

Hex values and their Base64 encodings
HexBytesBase64
001AA==
FF1/w==
00 002AAA=
FF FF2//8=
00 00 003AAAA
FF FF FF3////
48 69 ("Hi")2SGk=
48 65 6C 6C 6F ("Hello")5SGVsbG8=
DE AD BE EF43q2+7w==
01 02 03 04 055AQIDBAU=

Why Is My Base64 Different From Another Tool's?

Usually the other tool encoded the hex characters instead of the bytes they stand for. The result is still valid Base64, just of the wrong thing, and it comes out about twice as long. It is easy to do by accident in a terminal:

Encoding the hex for Hello, wrong and right
CommandOutput
echo 48656c6c6f | base64 (wrong)NDg2NTZjNmM2Zgo=
echo 48656c6c6f | xxd -r -p | base64 (right)SGVsbG8=

If the lengths match but a few characters differ, check the output option. URL-safe Base64 swaps + and / for - and _ and leaves off the = signs. PEM and MIME add line breaks. The bytes are the same in all of them.

Converting a Hash or Checksum to Base64

The most common reason to convert hex to Base64 is a hash. Checksum tools print hashes in hex, but many websites and APIs ask for the same hash in Base64. The hash doesn't change, only how it's written. For example, the MD5 of an empty file is d41d8cd98f00b204e9800998ecf8427e in hex and 1B2M2Y8AsgTpgAmY7PhCfg== in Base64. The tool tells you when a pasted value is the size of a common hash.

Common hash sizes in hex and Base64
HashBytesHex digitsBase64 characters
MD5163224 (ends in ==)
SHA-1204028 (ends in =)
SHA-256326444 (ends in =)
SHA-5126412888 (ends in ==)

Hex to Base64 in Python, JavaScript and Other Languages

In code it is the same two steps: turn the hex into bytes, then Base64-encode the bytes. Each line turns 48656c6c6f into SGVsbG8=. Strip any spaces or 0x prefixes first.

Converting hex to Base64 in common languages
LanguageCode
Pythonbase64.b64encode(bytes.fromhex(h)).decode()
JavaScript (Node.js)Buffer.from(h, 'hex').toString('base64')
JavaScript (recent browsers)Uint8Array.fromHex(h).toBase64()
Java (17+)Base64.getEncoder().encodeToString(HexFormat.of().parseHex(h))
C# (.NET 5+)Convert.ToBase64String(Convert.FromHexString(h))
PHPbase64_encode(hex2bin($h))
Mac or Linux terminalecho 48656c6c6f | xxd -r -p | base64

The mistake to avoid in any language is calling the Base64 function on the hex string itself. Going the other way? See the Base64 to hex converter.

Frequently asked questions

Why is the Base64 shorter than the hex?

Hex spends 2 characters on every byte, Base64 spends 4 characters on every 3 bytes. So 64 hex digits (32 bytes) become 44 Base64 characters.

How do I convert a hex number like 1F4?

Add a 0 in front to make an even number of digits: 01F4 is the bytes 01 F4, which encode to AfQ=.

Which output option should I use?

Standard for most uses. URL-safe for web addresses, file names and login tokens. PEM for certificate and key files, MIME for email.

Can I paste a checksum with the file name after it?

Paste only the hash. Letters outside a–f are rejected, but a file name made only of a–f letters, such as cafe, would be read as extra bytes.

Sources