toWorthy

Base64 Encode/Decode — Complete Guide

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.

Table of contents

    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.

    Related posts

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

    Quick Response (QR) codes have become a familiar part of daily life. You see them on product packaging, event tickets, restaurant tables, and even billboards. But what exactly is a QR code, how does it work, and how can you create one safely for your business or project? This guide explains the essentials in clear, practical terms you can use immediately.…

    The Beginner’s Guide to Regular Expressions (Regex)

    Regular expressions, often shortened to regex or regexp, are one of the most powerful tools for working with text. They allow you to describe patterns that match sets of strings—whether you want to validate an email address, find phone numbers in a document, or extract hashtags from social media posts. While regex can look intimidating at first, with a little practice it becomes an essential skill for developers, analysts, and anyone who works with data.…

    The Science of Strong Passwords

    When people think about strong passwords, they often imagine random combinations of letters, numbers, and symbols - something like P@55w0rD!. While complexity is important, modern security research shows that length is an even more critical factor.…