Hex to Base64 Converter
Hex to Base64 converter: paste hex bytes and get them as Base64, standard or URL-safe. DEADBEEF becomes 3q2+7w==. The input can have 0x, spaces or colons, and the byte count is shown for both forms. Swap converts Base64 back to hex, and nothing leaves your browser.
Hex to Base64: bytes first, then six bits at a time
To convert hex to Base64, read each pair of hex digits as one byte, then encode the bytes with Base64. The encoder takes the bytes three at a time, lays their 24 bits side by side and cuts them into four groups of six bits, and each group picks one character from a 64-character alphabet. The hex digits are only a way of writing the bytes, so the conversion never goes through decimal or text.
Four hex digits carry 16 bits and three bytes carry 24, so the digit boundaries and the Base64 boundaries do not line up. They meet again every 12 hex digits, which is every 8 Base64 characters. That is why the tool works on whole bytes and why a hex string must have an even number of digits: half a byte has no Base64 form.
A worked example: DEADBEEF is 3q2+7w==
DEADBEEF is four bytes, DE AD BE EF. The first three make one full group and the last one is a short group. The table follows the full group through the bits, and the last row shows how a single leftover byte is padded.
| Group | Bytes | Bits | Six-bit values | Characters |
|---|---|---|---|---|
| 1 | DE AD BE | 11011110 10101101 10111110 | 55, 42, 54, 62 | 3q2+ |
| 2 | EF | 11101111 (+ 0000 added) | 59, 48 | 7w (padded: 7w==) |
| Result | DE AD BE EF | 3q2+7w== |
The last byte has only eight bits, not twelve, so the encoder adds four zero bits to make two six-bit groups, writes two characters and fills the rest with two = signs. The padding is a signal of how many bytes were in the last group: one = means two bytes, two = means one byte, none means a multiple of three. The same rules in reverse are on the Base64 decode page.
How long the Base64 comes out
The Base64 length depends only on the number of bytes: four characters for every started group of three, so 4 × ⌈n ÷ 3⌉ with padding. The hex is always twice the byte count. The table shows the common cases, where Base64 saves about a third over hex.
| Data | Bytes | Hex characters | Base64 with padding | Base64 without padding |
|---|---|---|---|---|
| MD5 digest | 16 | 32 | 24 | 22 |
| UUID | 16 | 32 | 24 | 22 |
| SHA-1 digest | 20 | 40 | 28 | 27 |
| SHA-256 digest | 32 | 64 | 44 | 43 |
| SHA-512 digest | 64 | 128 | 88 | 86 |
The saving is why digests and keys are often shown as Base64 in headers, certificates and configuration files, while hex remains the choice where people compare values by eye or paste them into command lines.
A real digest: SHA-256 of an empty string
The SHA-256 digest of the empty string is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 in hex. It is 64 characters, 32 bytes. Pasted into the converter it becomes 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=, which is 44 characters, the last one an = because 32 is not a multiple of three (32 = 30 + 2, leaving a short group of two bytes). Both strings name the same 256 bits; a system that expects one will not accept the other, which is usually the reason for the conversion.
This also gives you a way to confirm that the tool works: take any digest from a command such as sha256sum, convert it, and compare it with the Base64 digest that another tool prints for the same data. For the opposite direction and for longer digests, see Base64 to hex.
Signatures in headers: hex here, Base64 there
A keyed hash (HMAC) is the same bytes whichever way it is written, but services pick a text form. GitHub, for instance, sends its webhook signature as hex after the prefix sha256=, while Shopify sends the HMAC as Base64 in the X-Shopify-Hmac-Sha256 header. If your code computes the digest in one form and the header carries the other, the comparison fails even though the bytes match. Converting one of them settles which side is at fault.
Compare signatures in constant time in real code, and decode both to bytes first so that letter case and padding cannot cause a mismatch. Hex is not case sensitive and Base64 is, which is one more reason to compare bytes and not text.
UUIDs as short identifiers
A UUID is 16 bytes written as 32 hex digits with hyphens. Without the hyphens it can be turned into 22 Base64 characters, a third shorter. The UUID 123e4567-e89b-12d3-a456-426614174000 becomes Ej5FZ+ibEtOkVkJmFBdAAA== with standard padding, Ej5FZ-ibEtOkVkJmFBdAAA in the URL-safe alphabet without padding. Systems that store identifiers in compact form often use the second.
Remove the hyphens before converting, or let the converter skip them: it accepts hyphens, colons and spaces between bytes. Keep the original order of the bytes. Some databases store a UUID with the first three groups byte-swapped, so the hex you copy from there is not the textual UUID, and Base64 of it will not match the identifier elsewhere.
URL-safe output for links and file names
Standard Base64 uses + and /, which have a meaning in URLs and are not allowed in file names. The URL-safe alphabet in RFC 4648 section 5 replaces them with - and _. A hex pair such as FB FF shows the difference, being +/8= in the standard alphabet and -_8= in the URL-safe one. Pick the alphabet in the options above. The padding = can be dropped in many places, such as JWT segments, but only where the receiver is known to accept unpadded input.
Hex text is not bytes: why some results look wrong
The most frequent error is encoding the hex as a string. If a script runs Base64 on the text DEADBEEF it encodes eight ASCII characters, which are the bytes 44 45 41 44 42 45 45 46, and returns REVBREJFRUY=. The correct conversion treats the hex as four bytes and returns 3q2+7w==.
| What is encoded | Bytes | Base64 |
|---|---|---|
| Hex converted to bytes (this page) | DE AD BE EF | 3q2+7w== |
| The hex written as text | 44 45 41 44 42 45 45 46 | REVBREJFRUY= |
You can recognize the text version: it is always twice as long, and its Base64 begins with the characters that stand for ASCII letters (REV for DE, for example). If your result is twice as long as expected, this is the likely cause.
What the converter accepts as hex
The input can be written as plain digits (48656C6C6F), spaced pairs (48 65 6C 6C 6F), with a prefix (0x48 0x65), as C escapes (\x48\x65), as percent escapes (%48%65), colon-separated (48:65:6C, the way tools print fingerprints) or on several lines. Upper and lower case are the same. Anything else, such as the letter G or a stray quote, is reported with its position and the conversion stops, so a wrong character cannot silently change the data.
On the command line
With xxd and base64 installed, echo DEADBEEF | xxd -r -p | base64 returns 3q2+7w==, and OpenSSL does the same with openssl base64 at the end of the pipe. The -r -p flags tell xxd to reverse a plain hex dump into bytes. Do not add -n or leave out the hex-to-bytes step: echo -n DEADBEEF | base64 encodes the text and returns REVBREJFRUY=.
echo DEADBEEF | xxd -r -p | base64
# 3q2+7w==
echo DEADBEEF | xxd -r -p | openssl base64
# 3q2+7w==In JavaScript and PowerShell
In Node.js, Buffer.from(hex, "hex").toString("base64") does both steps, and "base64url" gives the URL-safe form without padding. In the browser there is no hex helper, so build the bytes first. In Windows PowerShell 5.1, convert each pair with the Convert class and pass the array to ToBase64String; on PowerShell 7 and .NET 5 or later, Convert.FromHexString does it in one call. See Buffers and character encodings in the Node.js documentation.
Buffer.from("DEADBEEF", "hex").toString("base64"); // "3q2+7w=="
Buffer.from("DEADBEEF", "hex").toString("base64url"); // "3q2-7w"const hex = "DEADBEEF";
const bytes = Uint8Array.from(hex.match(/../g), (h) => parseInt(h, 16));
btoa(String.fromCharCode(...bytes)); // "3q2+7w=="$hex = 'DEADBEEF'
$bytes = [byte[]]($hex -split '(..)' -ne '' | ForEach-Object { [Convert]::ToByte($_, 16) })
[Convert]::ToBase64String($bytes) # 3q2+7w==Long keys: line wrapping and why a raw key is not a PEM file
Base64 that goes into email is wrapped at 76 characters per line, and the Base64 body of a PEM file at 64. The converter produces one unbroken line, which is what JSON, headers and environment variables want. If a format needs wrapped lines, insert the line breaks after conversion; the data does not change, and decoders ignore the breaks.
A PEM file is more than Base64 of key bytes, though. It is Base64 of a DER structure, with the lengths and type markers of ASN.1, between BEGIN and END lines. Converting only the raw key bytes, such as a 32-byte secret in hex, gives a valid Base64 string but not a PEM key, and a tool expecting PEM will reject it. Raw secrets are used as Base64 text directly in many configuration files, so check which form the consumer wants before converting.
Checking the result
Use the Swap button to convert the output back to hex; the hex that returns should be the one you started with, with the same number of bytes. The status line under the result shows the byte count and the output length, which you can compare with the length table above. If the byte count is not what you expect, a digit was dropped or added on the way.
Typical mistakes when converting hex to Base64
Besides encoding the text, three problems come up often. A leading zero lost in a spreadsheet or a number type turns 0AFF into AFF, which is an odd number of digits and a different value. A hash taken from a UI may include a label such as sha256: that is not hex and has to be removed. And a hex string pasted with the 0x prefix on every byte is fine, but with it only at the front 0xDEADBEEF is read as one prefix and four bytes. If the output looks plausible but fails in the receiving system, decode it with Base64 decode and compare the bytes.
Questions about converting hex to Base64
How do I convert hex to Base64?
Turn each pair of hex digits into one byte, then encode the bytes as Base64, three bytes at a time. DEADBEEF is the bytes DE AD BE EF, which become 3q2+7w==.
Why is the Base64 shorter than the hex?
Hex spends two characters per byte and Base64 spends four per three bytes, about 1.33 per byte. A 32-byte SHA-256 digest is 64 hex characters but 44 in Base64, or 43 without padding.
Why does my result differ from encoding the hex string as text?
Base64 of the text DEADBEEF encodes the eight characters D, E, A, D, B, E, E, F as ASCII bytes and gives REVBREJFRUY=. Converting the hex to bytes first gives 3q2+7w==. Only the second one is the data the hex stands for.
Does the converter remove the trailing equals signs?
Not by default. Padding is part of standard Base64, and the = signs bring the output to a multiple of four characters. Choose the URL-safe alphabet if you need - and _ instead of + and /, and strip the = signs yourself where the receiving system allows unpadded input.
What happens if my hex has an odd number of digits?
The converter reports it and names the last digit that has no partner. Eight bits make a byte, so the digits must come in pairs; 0xABC is not a whole number of bytes, and padding it on the left (0ABC) or the right (ABC0) gives different data.
