toWorthy
Practical Guide
← Back to all articles

Base64 Encode/Decode — Complete Guide

3 min read 445 words 0% read

Base64 encoding is one of the most common methods to represent binary data as text. If you’ve ever worked with images in HTML, JSON APIs, or JWT tokens, chances are you’ve already seen Base64 strings - long sequences of characters like SGVsbG8gd29ybGQh.

In this guide, you’ll learn everything about Base64 encoding and decoding: what Base64 is, how it works, why it’s used in development, and how to encode/decode in JavaScript and PHP with practical code examples.

What is Base64?

Base64 is a method of encoding binary data into text using only 64 ASCII characters. The character set consists of uppercase letters (A–Z), lowercase letters (a–z), numbers (0–9), and two symbols: + and /. The padding character = is sometimes added to make the length divisible by 4.

Base64 was originally created to transfer binary data safely over text-only systems (like email). Today it is widely used in web development, APIs, and data storage.

How Base64 Encoding Works

Base64 encoding converts every 3 bytes (24 bits) of binary data into 4 ASCII characters:

  1. Take 3 bytes of input (for example, "ABC").
  2. Convert them into binary (24 bits).
  3. Split into 4 groups of 6 bits.
  4. Map each group to the Base64 alphabet.
  5. If the input is not divisible by 3, add = padding.

Example: ABCQUJD

Base64 Encode/Decode in JavaScript

// Encode to Base64
const text = "Hello World!";
const encoded = btoa(text);
console.log(encoded); // SGVsbG8gV29ybGQh

// Decode from Base64
const decoded = atob(encoded);
console.log(decoded); // Hello World!

In Node.js you can also use Buffer:

const text = "Hello World!";
const encoded = Buffer.from(text).toString('base64');
console.log(encoded); // SGVsbG8gV29ybGQh

const decoded = Buffer.from(encoded, 'base64').toString('utf8');
console.log(decoded); // Hello World!

Base64 Encode/Decode in PHP

<?php
$text = "Hello World!";

// Encode to Base64
$encoded = base64_encode($text);
echo $encoded; // SGVsbG8gV29ybGQh

// Decode from Base64
$decoded = base64_decode($encoded);
echo $decoded; // Hello World!
?>

Common Use Cases

  1. Embedding images in HTML/CSS: <img src="data:image/png;base64,...">
  2. APIs and JSON: safe transmission of binary data inside JSON payloads
  3. JWT tokens: headers and payloads are Base64 encoded
  4. Data transfer: storing or sending binary files as text

Misconceptions

Base64 is not encryption. It’s just an encoding method, and anyone can decode Base64 easily.

Other important notes:

  1. Base64 increases data size by about 33%.
  2. It’s not suitable for storing large files (for example, images in a database).
  3. For sensitive data, use real encryption (AES, RSA, etc.).

FAQ

What is Base64 used for?

To represent binary data (like images, JSON, or files) as text that can be safely transmitted or stored.

Is Base64 secure?

No. It’s reversible and should not be used for passwords or sensitive information.

Why does Base64 end with = ?

The = is padding to make the string length a multiple of 4.

How much larger is Base64 data?

About 33% bigger than the original binary data.

Can I use Base64 in URLs?

Yes, but replace + with - and / with _ (URL-safe Base64).

Conclusion

Base64 is everywhere in development — from JWT tokens to APIs and embedded images. By understanding how it works, you can use it effectively and avoid common pitfalls.

You can also try our free Base64 Encoder/Decoder Tool. Paste your text or file and get the result instantly — secure, fast, and running entirely in your browser.

Previous article

What Is a QR Code? The Complete Beginner’s Guide

Next article

CORS Explained — A Complete Guide for Web Developers

Want practical tools next to these guides?

Try monitoring, webhook debugging and security helpers directly in toWorthy.

Comments (0)

No approved comments yet.

Sign in to write a comment.

Related posts

Why ?v=md Is Useful in the Age of AI

The web was built for humans. Pages are rendered in HTML, styled with CSS, enhanced with JavaScript, and surrounded by navigation, footers, and interface components. But today, content is not consumed only by humans. AI systems read it too.…