How hex to Base64 conversion works
Convert each pair of hex digits to a byte, group the bytes in threes, split each 24-bit group into four 6-bit values, and look up each value in the Base64 alphabet. The whole process passes through binary: hex digits map to 4 bits each, and Base64 characters map to 6 bits each.
- Split the hex string into pairs of digits. Each pair is one byte (8 bits).
- Take the bytes in groups of 3. Each group holds 24 bits.
- Divide the 24 bits into four chunks of 6 bits.
- Map each 6-bit value (0 to 63) to a character in the Base64 alphabet.
- Pad the output with
=signs when the last group has fewer than 3 bytes.
Example: convert 48656C6C6F20576F726C64 (Hello World)
The 11 bytes are 48 (H), 65 (e), 6C (l), 6C (l), 6F (o), 20 (space), 57 (W), 6F (o), 72 (r), 6C (l) and 64 (d). The first three groups hold 3 bytes each, and the last group holds 2 bytes.
The first group, 48 65 6C, is 01001000 01100101 01101100. As 24 bits that is 010010000110010101101100. Split into four 6-bit chunks: 010010 000110 010101 101100, which are the values 18, 6, 21 and 44. The Base64 characters are S, G, V, s.
| Group | Hex bytes | Base64 |
|---|---|---|
| 1 | 48 65 6C | SGVs |
| 2 | 6C 6F 20 | bG8g |
| 3 | 57 6F 72 | V29y |
| 4 | 6C 64 (2 bytes) | bGQ= |
The last group has only 2 bytes (16 bits), so the encoder pads the bits with zeros to fill the third 6-bit chunk and appends one = sign. Result: 48656C6C6F20576F726C64 → SGVsbG8gV29ybGQ=
The hex string has 22 characters, and the Base64 output has 16 (including the padding). Base64 is always shorter than hex for the same data, because it packs more information per character (6 bits against 4 bits).
Padding with =
Base64 encodes 3 bytes as 4 characters. When the input length is not a multiple of 3, the encoder adds = padding to make the output length a multiple of 4.
| Leftover bytes | Characters | Padding | Hex | Base64 |
|---|---|---|---|---|
| 0 (3 bytes) | 4 | None | 4D616E | TWFu |
| 1 byte | 2 + == | == | 4D | TQ== |
| 2 bytes | 3 + = | = | 4D61 | TWE= |
For n bytes of input, the padded output has 4 × ⌈n ÷ 3⌉ characters. Base64 output is about 33% larger than the raw bytes, but 33% smaller than the same data in hex. A 32-byte SHA-256 hash takes 64 hex characters or 44 Base64 characters (including one =).
Base64 alphabet
The standard Base64 alphabet defined in RFC 4648 has 64 characters: A through Z (values 0 to 25), a through z (26 to 51), 0 through 9 (52 to 61), + (62) and / (63). The = sign is a padding marker, not part of the 64-character set.
Each 6-bit binary value from 000000 to 111111 maps to one character. The table is the same across all standard Base64 implementations. Tap a tile to see its bits, or switch to the URL-safe variant.
| Value | Char |
|---|---|
| 0 | A |
| 1 | B |
| 2 | C |
| 3 | D |
| 4 | E |
| 5 | F |
| 6 | G |
| 7 | H |
| 8 | I |
| 9 | J |
| 10 | K |
| 11 | L |
| 12 | M |
| 13 | N |
| 14 | O |
| 15 | P |
| Value | Char |
|---|---|
| 16 | Q |
| 17 | R |
| 18 | S |
| 19 | T |
| 20 | U |
| 21 | V |
| 22 | W |
| 23 | X |
| 24 | Y |
| 25 | Z |
| 26 | a |
| 27 | b |
| 28 | c |
| 29 | d |
| 30 | e |
| 31 | f |
| Value | Char |
|---|---|
| 32 | g |
| 33 | h |
| 34 | i |
| 35 | j |
| 36 | k |
| 37 | l |
| 38 | m |
| 39 | n |
| 40 | o |
| 41 | p |
| 42 | q |
| 43 | r |
| 44 | s |
| 45 | t |
| 46 | u |
| 47 | v |
| Value | Char |
|---|---|
| 48 | w |
| 49 | x |
| 50 | y |
| 51 | z |
| 52 | 0 |
| 53 | 1 |
| 54 | 2 |
| 55 | 3 |
| 56 | 4 |
| 57 | 5 |
| 58 | 6 |
| 59 | 7 |
| 60 | 8 |
| 61 | 9 |
| 62 | + |
| 63 | / |
Base64URL and other variants
Base64URL (URL-safe)
Base64URL replaces + with - and / with _, so the encoded string is safe in URLs, file names and JSON Web Tokens (JWT). RFC 4648 defines this variant. Most JWT libraries use Base64URL without padding by default.
MIME Base64
Email (MIME) wraps Base64 output at 76 characters per line. Privacy-Enhanced Mail (PEM) files, such as TLS certificates and private keys, wrap at 64 characters per line and add -----BEGIN ...----- headers.
No-padding Base64
Some protocols and APIs drop the trailing = characters, because the decoder can infer the original length from the output length. Use the “No padding” toggle in the converter above for these values.
Why convert hex to Base64
Hex and Base64 both represent binary data as printable text, but they serve different purposes.
| Property | Hex | Base64 |
|---|---|---|
| Characters per byte | 2 | about 1.33 |
| Character set | 0–9, A–F (16) | A–Z, a–z, 0–9, +, / (64) |
| Size against raw bytes | +100% | +33% |
| Readability | Each byte is obvious (C0 = 192) | Bytes are not visible in the output |
Base64 is shorter. A 256-bit key takes 64 hex characters or 44 Base64 characters. For large payloads over HTTP, email or in JSON fields, the size difference matters.
Text-based protocols need Base64. HTTP headers, email bodies, JSON and XML cannot carry arbitrary binary bytes. Base64 produces only printable ASCII characters, which pass through any text channel without corruption.
Hex is easier to inspect. Each byte maps to exactly two hex digits, so an engineer can read individual byte values from a hex dump. Base64 hides the byte boundaries. Use hex for debugging and packet inspection, and Base64 for transmission, storage and embedding.
Where hex to Base64 appears
Cryptographic keys and certificates
Private keys, public keys and X.509 certificates are stored as Base64 in PEM files. Tools such as OpenSSL output hex fingerprints and Base64-encoded certificate bodies. Converting a hex-encoded key to Base64 is a common step when you import keys into applications or configuration files.
Hashes and signatures
A SHA-256 digest is 32 bytes, shown as 64 hex digits or 44 Base64 characters. Many APIs accept either format, and some (such as Subresource Integrity in HTML) require Base64. Converting the hex output of a command-line hashing tool to Base64 is a frequent task.
JSON Web Tokens (JWT)
A JWT has three Base64URL-encoded segments: header, payload and signature. The signature is often computed from a hex-encoded secret or key, and the result must be in Base64URL before it goes into the token.
Data URIs
Embedding binary data in HTML or CSS uses the data: URI scheme with Base64 encoding, such as data:image/png;base64,iVBORw0KGgo.... Converting hex bytes to Base64 produces the value that goes after the comma.
Email attachments (MIME)
The Multipurpose Internet Mail Extensions (MIME) standard encodes binary attachments as Base64 text inside email messages. Binary data must be Base64-encoded before transmission, whether it starts as hex or as raw bytes.
Base64 to hex (reverse conversion)
The reverse process decodes Base64 characters back into bytes and writes each byte as two hex digits. The converter above handles both directions: paste Base64 in the second field to get hex.
- Map each Base64 character to its 6-bit value using the alphabet table.
- Join the 6-bit values into a continuous bit stream.
- Split the bit stream into 8-bit bytes and discard any trailing pad bits.
- Write each byte as a two-digit hex value.
Example: decode SGVsbG8= to hex
S = 18 = 010010, G = 6 = 000110, V = 21 = 010101, s = 44 = 101100, b = 27 = 011011, G = 6 = 000110 and 8 = 60 = 111100. The = is padding. The bit stream 010010 000110 010101 101100 011011 000110 111100 splits into the bytes 01001000 01100101 01101100 01101100 01101111. In hex that is 48 65 6C 6C 6F, the ASCII text “Hello”.
Hex to Base64 in programming
Pick a language to see the call and its output for your value.
Python
import base64
hex_string = '48656C6C6F20576F726C64'
raw_bytes = bytes.fromhex(hex_string)
b64 = base64.b64encode(raw_bytes).decode()
print(b64) # SGVsbG8gV29ybGQ=bytes.fromhex() converts the hex string to a bytes object, and base64.b64encode() returns the Base64-encoded bytes. Call .decode() to get a plain string. For the full hex-to-decimal guide, see hex to decimal in Python.
JavaScript
const hex = '48656C6C6F20576F726C64';
const bytes = hex.match(/.{2}/g).map(b => parseInt(b, 16));
const b64 = btoa(String.fromCharCode(...bytes));
console.log(b64); // SGVsbG8gV29ybGQ=parseInt(b, 16) converts each hex pair to a byte value, String.fromCharCode builds a binary string, and btoa() encodes it as Base64. For the full guide, see hex to decimal in JavaScript.
Java
import java.util.Base64;
import java.util.HexFormat;
public class HexToBase64 {
public static void main(String[] args) {
String hex = "48656C6C6F20576F726C64";
byte[] bytes = HexFormat.of().parseHex(hex);
String b64 = Base64.getEncoder().encodeToString(bytes);
System.out.println(b64); // SGVsbG8gV29ybGQ=
}
}HexFormat.of().parseHex() (Java 17 and later) parses the hex string into a byte array, and Base64.getEncoder().encodeToString() produces the Base64 output.
Bash and the Linux command line
echo '48656C6C6F20576F726C64' | xxd -r -p | base64
# SGVsbG8gV29ybGQ=xxd -r -p converts hex to raw bytes, and base64 encodes them. On macOS the same command works. On some systems, use base64 -w 0 to suppress line wrapping.
PowerShell
$hex = '48656C6C6F20576F726C64'
$bytes = [Convert]::FromHexString($hex) # PowerShell 7 and later
[Convert]::ToBase64String($bytes)
# SGVsbG8gV29ybGQ=Other languages
| Language | Method | Example |
|---|---|---|
| Go | hex.DecodeString + base64.StdEncoding.EncodeToString | Decode hex, encode bytes |
| PHP | base64_encode(hex2bin()) | base64_encode(hex2bin('48656C6C6F')) |
| C# | Convert.FromHexString + Convert.ToBase64String | .NET 5 and later |
| Ruby | [hex].pack('H*'), then .pack('m0') | Packs hex into bytes |
Common mistakes
Odd number of hex digits
Each byte needs exactly two hex digits. The hex string “FFF” has 3 digits and does not map to a whole number of bytes. Pad it to “0FFF” (2 bytes) or “000FFF” (3 bytes), depending on the intended value.
Encoding the hex text instead of the bytes
Base64-encoding the ASCII string “48656C6C6F” gives “NDg2NTZDNkM2Rg==”, not “SGVsbG8=”. The first encodes the characters 4, 8, 6, 5 and so on as text. The second encodes the bytes 0x48, 0x65, 0x6C, 0x6C and 0x6F, which spell “Hello”. Always convert hex digits to bytes first, then Base64-encode the bytes.
Mixing Base64 and Base64URL alphabets
A Base64 string with + and / fails in a Base64URL decoder that expects - and _. Check which variant your system uses before you paste values between tools.
Ignoring or mishandling padding
Some decoders require = padding and reject input without it. Others reject input that includes it. Match the padding setting to the system that will read the output. The converter above has a toggle for this.
Frequently asked questions
How do you convert hex to Base64?
= when the last group has fewer than 3 bytes.Is Base64 encryption?
Why is Base64 output larger than the original data?
What does the = at the end mean?
= sign is a padding character that fills the output to a multiple of 4 characters. One = means the last group had 2 bytes (16 bits), and two == mean it had 1 byte (8 bits). No = means the input was an exact multiple of 3 bytes.What is the difference between Base64 and Base64URL?
+ with - and / with _, and it usually omits the = padding. RFC 4648 defines both variants. Base64URL is safe for URLs, file names and JWTs, where +, / and = have special meanings.Is Base64 to hex conversion reversible?
How much shorter is Base64 than hex?
Can hex to Base64 handle binary data that is not text?
Related tools
- Hex to decimal converter: convert hex to decimal with digit-by-digit steps.
- Hex to binary converter: see the bit pattern of any hex value.
- Hex to text converter: decode hex bytes into ASCII or UTF-8 text.
- Decimal to hex converter: convert decimal to hex with the division method.
- Hex to octal converter: regroup bits from fours into threes.