Base64 Encoding: What Developers Need to Know

Encoded text on screen

If you've ever seen a string like "SGVsbG8gV29ybGQh" and wondered what in the world that is, you've encountered Base64. It's not encryption, it's not compression, it's encoding—and understanding the difference matters.

What Base64 Actually Is

Base64 is a way to represent binary data using only ASCII characters. It takes 3 bytes (24 bits) and converts them into 4 characters from a 64-character alphabet (A-Z, a-z, 0-9, +, /).

The "=" or "==" at the end? That's padding, because Base64 works on 3-byte chunks. If your data isn't divisible by 3, padding fills in the gaps.

Why 64 Characters?

64 is 2^6, which means each character represents exactly 6 bits. Three bytes = 24 bits = four 6-bit groups = four Base64 characters. The math works out cleanly.

When to Use Base64

Embedding Images in CSS/HTML

Data URIs use Base64 to embed small images directly in CSS or HTML: "background-image: url(data:image/png;base64,iVBORw...)"

Transmitting Binary Over JSON

JSON is text-only. If you need to include binary data (like a file upload) in a JSON payload, Base64 is a common solution.

API Authentication

HTTP Basic Auth encodes credentials as Base64. It's not encryption—anyone can decode it—so always use HTTPS.

When NOT to Use Base64

Base64 increases data size by ~33%. It should never be used for compression or "security." It's also slower to encode/decode than raw bytes.

If you're thinking "I'll Base64-compress my data," stop. Use gzip or Brotli instead.

Related Tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.