Hex to Decimal

Hex to Base64 converter

Paste hex bytes to get Base64, or paste Base64 to get hex. Use it for API keys, tokens, hashes and data URIs. The sections below cover the conversion method, padding, the alphabet, Base64URL, common uses, the reverse process, code in several languages and common mistakes.

3 bytes in, 4 characters out 24 bits split into four 6-bit values

Bytes
Base64 length
Size change
Data URI

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.

  1. Split the hex string into pairs of digits. Each pair is one byte (8 bits).
  2. Take the bytes in groups of 3. Each group holds 24 bits.
  3. Divide the 24 bits into four chunks of 6 bits.
  4. Map each 6-bit value (0 to 63) to a character in the Base64 alphabet.
  5. 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.

GroupHex bytesBase64
148 65 6CSGVs
26C 6F 20bG8g
357 6F 72V29y
46C 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 bytesCharactersPaddingHexBase64
0 (3 bytes)4None4D616ETWFu
1 byte2 + ====4DTQ==
2 bytes3 + ==4D61TWE=

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.

ValueChar
0A
1B
2C
3D
4E
5F
6G
7H
8I
9J
10K
11L
12M
13N
14O
15P
ValueChar
16Q
17R
18S
19T
20U
21V
22W
23X
24Y
25Z
26a
27b
28c
29d
30e
31f
ValueChar
32g
33h
34i
35j
36k
37l
38m
39n
40o
41p
42q
43r
44s
45t
46u
47v
ValueChar
48w
49x
50y
51z
520
531
542
553
564
575
586
597
608
619
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.

PropertyHexBase64
Characters per byte2about 1.33
Character set0–9, A–F (16)A–Z, a–z, 0–9, +, / (64)
Size against raw bytes+100%+33%
ReadabilityEach 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.

  1. Map each Base64 character to its 6-bit value using the alphabet table.
  2. Join the 6-bit values into a continuous bit stream.
  3. Split the bit stream into 8-bit bytes and discard any trailing pad bits.
  4. 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

LanguageMethodExample
Gohex.DecodeString + base64.StdEncoding.EncodeToStringDecode hex, encode bytes
PHPbase64_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?
Split the hex string into byte pairs, group the bytes in threes, divide each 24-bit group into four 6-bit values, and map each value to a Base64 character. Pad the output with = when the last group has fewer than 3 bytes.
Is Base64 encryption?
No. Base64 is an encoding, not encryption. Encoding converts data to a different representation without a secret key. Anyone can decode Base64 back to the original bytes. Encryption needs a key and makes the data unreadable without it.
Why is Base64 output larger than the original data?
Base64 uses 4 characters to represent 3 bytes, which adds 33% overhead. Each character carries 6 bits of data, while a raw byte carries 8 bits. In return, Base64 output contains only printable ASCII characters, which pass safely through text-based systems.
What does the = at the end mean?
The = 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?
Base64URL replaces + 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?
Yes. The conversion is lossless in both directions. Decoding Base64 back to bytes and writing each byte as two hex digits produces the original hex string with no data loss.
How much shorter is Base64 than hex?
Base64 uses about 1.33 characters per byte, while hex uses 2 characters per byte. For a 32-byte (256-bit) value, hex gives 64 characters and Base64 gives 44 (with padding). Base64 is 33% shorter than hex for the same data.
Can hex to Base64 handle binary data that is not text?
Yes. The conversion treats the hex string as raw bytes, not as text. Any byte value from 00 to FF encodes correctly. The result is a text-safe representation of arbitrary binary data, including images, keys and compiled files.