Base64EncodingBeginner

Base64 Encoding Explained for Beginners

March 8, 2026·5 min read

Base64 shows up in email payloads, JWT segments, HTTP headers, data URLs, and API integrations. It is easy to use mechanically without understanding what problem it solves, which leads to confusion when payloads fail to decode or text is corrupted. This article explains Base64 in the context where developers actually meet it.

1. What Problem Does Base64 Solve?

Computers store everything as binary data. When binary data needs to travel through systems designed for text (HTTP headers, email, XML, JSON), raw bytes can be misinterpreted or corrupted. Old email protocols like SMTP were designed for 7-bit ASCII text — sending a binary image directly would corrupt it because many byte values have special meanings in ASCII.

Base64 solves this by mapping arbitrary binary data to a safe, printable ASCII character set that can pass through any text-based medium without alteration.

2. The Base64 Character Set

A-Z  (26 uppercase)  → values 0–25
a-z  (26 lowercase)  → values 26–51
0-9  (10 digits)     → values 52–61
+    (plus)          → value 62
/    (slash)         → value 63
=    (equals)        → padding (not a value)

Since 2^6 = 64, each Base64 character encodes exactly 6 bits. Every 3 input bytes (24 bits) become 4 Base64 characters — a 33% size overhead.

Size Comparison: 3 bytes → 4 characters (33% overhead)Original (3 bytes)24 bitsencodeBase64 (4 chars)32 bits

3. How the Algorithm Works

Encoding "Man" step by step:

Base64 Encoding Process: "Man" → "TWFu"Step 1: ASCII to BinaryM (77)a (97)n (110)Step 2: 8-bit Binary010011010110000101101110Step 3: Split into 6-bit010011010110000101101110Step 4: Map to Base64TWFuValues: 19, 22, 5, 46
M=77→01001101  a=97→01100001  n=110→01101110
Concatenated: 010011010110000101101110
Split 6-bit:  010011 | 010110 | 000101 | 101110
Values:         19       22       5       46
Base64:          T        W        F       u
Result: "Man" → "TWFu"

Padding: if input is 1 byte, append ==; if 2 bytes, append =.

"M"   → "TQ=="
"Ma"  → "TWE="
"Man" → "TWFu"

4. Common Use Cases

Email attachments (MIME)

MIME encodes binary attachments as Base64 so they survive text-only SMTP servers.

Data URIs in HTML/CSS

Embed small images inline: <img src="data:image/png;base64,iVBOR..."/> eliminating HTTP requests.

JWT tokens

JWT sections (header.payload.signature) are Base64URL-encoded, a URL-safe variant replacing + with - and / with _.

HTTP Basic Auth

Authorization: Basic encodes "user:password" as Base64.

Binary data in JSON/XML

Embed file contents (PDFs, images) inside JSON API payloads as Base64 strings.

5. Implementation: Encode/Decode in the Browser

JavaScript provides built-in btoa (encode) and atob (decode). For Unicode text, encode to UTF-8 first:

// Encode (handles Unicode via TextEncoder)
function base64Encode(str) {
  const bytes = new TextEncoder().encode(str);
  let binary = '';
  bytes.forEach(b => binary += String.fromCharCode(b));
  return btoa(binary);
}

// Decode
function base64Decode(b64) {
  const binary = atob(b64);
  const bytes = new Uint8Array(binary.length);
  for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
  return new TextDecoder().decode(bytes);
}

// Simple ASCII (no Unicode)
const encoded = btoa('Hello, World!');  // SGVsbG8sIFdvcmxkIQ==
const decoded = atob(encoded);           // Hello, World!

6. Code Examples by Language

JavaScript

const encoded = btoa('Hello, World!');
// SGVsbG8sIFdvcmxkIQ==

const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
// Hello, World!

Python

import base64

encoded = base64.b64encode(b"Hello, World!")
# b'SGVsbG8sIFdvcmxkIQ=='

decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==')
# b'Hello, World!'

7. Security Warning

⚠️ Base64 is NOT encryption

Base64 is fully reversible with zero key — anyone can decode it instantly. Never use it to hide passwords, API keys, or sensitive data. Use proper encryption (AES-256, RSA) for that purpose.

8. Conclusion

Base64 is a fundamental encoding scheme for transferring binary data through text-based systems. Understanding its algorithm, 33% size overhead, and the critical distinction from encryption helps you use it correctly. Use the Base64 encoder/decoder to inspect and convert values locally in the browser.