Base64 vs Hex Encoding: When to Use Each
Learn the difference between Base64 and hexadecimal encoding, how each works, why they exist, and when one is a better choice than the other.
These two strings represent the same bytes:
SGVsbG8gV29ybGQ=
48656c6c6f20576f726c64
The first is Base64. The second is hexadecimal.
Decode either one and you get:
Hello World
Neither representation encrypts, compresses, or hashes the data. They simply turn bytes into text using different alphabets.
Hex Maps Each Byte to Two Characters
Hexadecimal uses sixteen symbols:
0 1 2 3 4 5 6 7 8 9 A B C D E F
One hex digit represents four bits, so a byte maps neatly to two digits:
binary: 01001000
0100 1000
hex: 4 8
The ASCII/UTF-8 bytes for Hello are:
48 65 6c 6c 6f
or without spaces:
48656c6c6f
That direct byte-to-two-digits mapping is why hex is convenient when humans need to inspect binary values.
Base64 Packs Three Bytes Into Four Characters
Standard Base64 uses a 64-character alphabet:
A-Z
a-z
0-9
+
/
Each Base64 symbol carries six bits of information. Three input bytes contain 24 bits, which divide cleanly into four six-bit values:
3 bytes
↓
24 bits
↓
4 × 6-bit values
↓
4 Base64 characters
When the input length is not a multiple of three bytes, standard padded
Base64 uses = characters to complete the final four-character group.
For example:
Hello
becomes:
SGVsbG8=
The exact alphabet and padding rules are defined by RFC 4648.
The Size Difference Is Predictable
Hex always uses two characters per byte.
Ignoring surrounding transport overhead:
1 byte → 2 hex characters
so the encoded text is twice the number of input bytes: 100% expansion.
Base64 encodes three bytes as four characters:
3 bytes → 4 Base64 characters
For sufficiently large input, that approaches 33⅓% expansion. Small inputs can have a larger percentage overhead because Base64 works in four-character blocks and may require padding.
For example:
| Input bytes | Hex characters | Padded Base64 characters |
|---|---|---|
| 1 | 2 | 4 |
| 3 | 6 | 4 |
| 100 | 200 | 136 |
| 1,000 | 2,000 | 1,336 |
This is why “a 100 KB file becomes 133 KB in Base64” is only an approximation. Exact encoded length depends on the number of input bytes and, for files, what the stated KB unit means.
Hex Is Convenient for Hashes and Low-Level Inspection
A SHA-256 digest is 32 bytes. In hex, it is always 64 characters:
e3b0c44298fc1c149afbf4c8996fb924
27ae41e4649b934ca495991b7852b855
The representation has a simple visual relationship with the underlying bytes: every two characters are one byte.
That is useful for hashes, checksums, fingerprints, memory dumps, protocol debugging, and other places where developers compare or inspect byte values.
Base64 can represent the same 32-byte digest more compactly. Hex is common here because compactness is often less important than convention and easy byte-level inspection.
Base64 Is Useful When Binary Data Must Travel as Text
Base64 is common when a text-oriented format needs to carry arbitrary bytes.
For example, JSON has strings but no native binary value:
{
"data": "SGVsbG8="
}
A protocol can define that string as Base64-encoded bytes.
The important phrase is the protocol can define it. JSON does not automatically Base64-encode binary data, and not every API should put files inside JSON. HTTP can transmit binary bodies directly, and multipart formats are often a better fit for file uploads.
Base64 is useful when the surrounding representation specifically needs text-safe binary data.
Base64URL Changes the Alphabet for URL-Safe Uses
Standard Base64 includes + and /, which are inconvenient in some URL
and filename contexts.
RFC 4648 defines a URL- and filename-safe variant that uses:
-
_
instead of:
+
/
Padding may also be omitted when the surrounding specification defines how the value is interpreted.
JWT is a familiar example. Its compact serialization uses Base64url for encoded segments, not ordinary padded Base64.
That distinction matters when copying values between libraries: Base64 and Base64url are closely related, but they are not always interchangeable as strings.
Neither Encoding Provides Security
This:
SGVsbG8=
may look opaque if you do not recognize it. It is still just an encoding
of Hello.
Anyone who knows the encoding can reverse it. The same is true for:
48656c6c6f
Encoding answers a representation problem. Encryption is designed to provide confidentiality when used correctly, while hashing is a one-way transformation used for different purposes.
Calling Base64 ‘encrypted’ confuses representation with security.
Choosing Between Them
The choice is usually straightforward.
Use hex when the byte-to-text mapping should be easy to inspect:
hashes
checksums
fingerprints
debugging
protocol fields
memory / binary inspection
Use Base64 when arbitrary bytes need a more compact text representation:
text-based protocol fields
certificates and key material in text formats
encoded payloads defined by a protocol
data URLs
Do not choose Base64 merely because the result is shorter than hex. If the surrounding protocol supports raw binary, raw bytes are smaller than either text encoding.
The Difference Is Representation Density
Hex represents four bits per character. Base64 represents six.
That explains most of the practical difference:
hex → simpler byte mapping, more text
Base64 → denser text, less obvious byte mapping
Both are reversible encodings. Neither protects the data. Neither changes what the bytes mean.
Choose hex when direct byte readability matters, and Base64 when the system needs a denser text-safe representation.
More Articles Like This

JSON Schema vs TypeScript Types: Do You Need Both?

Regex vs Parsing: When Pattern Matching Stops Being Enough

What Is a .dockerignore File? Why Docker Builds More Than You Think

What Is a Regular Expression, Actually? (Beyond the Cheat Sheet)
