BASE64
解码 & 编码
← Guides· 3 min read

Base64 vs Hex Encoding – What's the Difference?

Both Base64 and hex (hexadecimal) convert binary data to text — but they differ in efficiency, readability, and where they're used.

How They Work

Hex encoding converts each byte (8 bits) into two hexadecimal characters (0–9, a–f). Simple and predictable: 1 byte always becomes exactly 2 characters.

Base64 encoding processes 3 bytes at a time and converts them into 4 characters from a 64-character alphabet (A–Z, a–z, 0–9, +, /). More complex, but more efficient.

Size Comparison

InputOriginalHexBase64
"Hello"5 bytes10 chars (+100%)8 chars (+60%)
"Hello, World!"13 bytes26 chars (+100%)20 chars (+54%)
1 KB file1,024 bytes2,048 chars (+100%)1,368 chars (+34%)
1 MB file1,048,576 bytes2,097,152 chars (+100%)1,398,102 chars (+33%)

Base64 is consistently ~25% smaller than hex for the same data. For large files or high-volume APIs, this difference adds up.

Example: Same Data, Both Formats

Input bytes: [72, 101, 108, 108, 111]  →  "Hello"

Hex:    48656c6c6f           (10 chars)
Base64: SGVsbG8=             (8 chars)

When to Use Each

Use Base64 when:

  • Embedding binary in JSON, XML, or HTML
  • Sending files over HTTP APIs
  • Creating data URIs for images
  • Storing binary in YAML (Kubernetes secrets)
  • JWT tokens and OAuth payloads
  • Email attachments (MIME)

Use Hex when:

  • Displaying cryptographic hashes (MD5, SHA256)
  • Representing memory addresses or byte values
  • Color codes in CSS (#ff5733)
  • Network packet inspection
  • Debugging binary protocols
  • When byte-by-byte readability matters

Readability

Hex wins on readability for raw bytes — each pair of hex characters maps exactly to one byte, making it easy to inspect individual bytes. Base64 mixes bits across character boundaries, making it harder to read manually.

For example, a SHA256 hash is always shown as 64 hex characters — this is a universal convention because developers need to compare and inspect hashes. Base64 hashes exist (some APIs use them) but are harder to spot-check.

Converting Between Them in Python

import base64

data = b"Hello"

# To hex
hex_str = data.hex()           # → "48656c6c6f"

# To Base64
b64_str = base64.b64encode(data).decode()  # → "SGVsbG8="

# Hex → Base64
b64_from_hex = base64.b64encode(bytes.fromhex(hex_str)).decode()

# Base64 → Hex
hex_from_b64 = base64.b64decode(b64_str).hex()

Need to encode or decode Base64?

Use our free online tool — paste any text and convert instantly, no code required.

Open Base64 Converter →

Frequently Asked Questions

Is Base64 more efficient than hex?

Yes. Base64 expands data by ~33% (3 bytes → 4 characters), while hex doubles the size (1 byte → 2 characters). For the same binary data, Base64 output is about 25% smaller than hex.

When should I use hex instead of Base64?

Use hex when human readability of raw bytes matters — like displaying cryptographic hashes (MD5, SHA256), memory addresses, or color codes. Hex is easier to inspect byte-by-byte than Base64.

Which is used for passwords and hashing?

Both are used. SHA256 hashes are commonly displayed as hex (64 hex characters). bcrypt and some JWT implementations use Base64. The choice depends on the library and protocol standard.