Base64 Converter

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. This tool allows you to encode text or files into Base64 and decode Base64 strings back to their original form, all within your browser.

Encode Text or File to Base64
OR

What is Base64?

Base64 is a group of binary-to-text encoding schemes that represent binary data (more specifically, a sequence of 8-bit bytes) in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding. It is designed to carry data stored in binary formats across channels that only reliably support text content.

The Base64 alphabet contains 64 basic ASCII characters. The standard character set includes `A-Z`, `a-z`, `0-9`, and the symbols `+` and `/`. To handle data that isn't a multiple of three bytes, padding is used. The `=` character is used as a special padding symbol. For more detailed information, you can visit the Base64 Wikipedia page.

How Does Base64 Work?

The encoding process converts binary data into a text format. Here's a simplified breakdown of the steps involved:

  1. Grouping: The original binary data is broken into chunks of 3 bytes (24 bits).
  2. Splitting: Each 24-bit chunk is then divided into four 6-bit groups.
  3. Mapping: Each 6-bit group is mapped to one of the 64 characters in the Base64 alphabet. A 6-bit number can represent values from 0 to 63, which corresponds to the 64 characters.
  4. Padding: If the last group of binary data has fewer than 3 bytes, it's padded with zero bits to form a complete 6-bit group. The resulting Base64 string is then padded with one or two `=` characters to indicate how many bytes were missing from the original data.

Decoding is the reverse process: it takes the Base64 string, maps each character back to its 6-bit value, reassembles them into 3-byte (24-bit) groups, and reconstructs the original binary data.

Common Use Cases

Base64 encoding is commonly used in a number of applications:

  • Data URLs: Embedding images or other files directly into HTML or CSS files using the `data:image/png;base64,...` scheme. This can reduce the number of HTTP requests a browser needs to make.
  • Email Attachments: The SMTP protocol, which handles email transport, was originally designed for 7-bit ASCII text. Base64 is used to encode binary attachments so they can be safely transmitted.
  • Storing Binary Data in Text Fields: Storing small binary objects (like icons or configuration data) in text-based formats like JSON or XML.
  • Basic Authentication: The HTTP Basic Authentication scheme sends usernames and passwords in the format `username:password`, encoded with Base64. Note: This is not a form of security, as Base64 is easily reversible. It's merely an encoding to ensure the credentials are transmitted in a format compatible with HTTP headers.

Base64 in Programming Languages

Most modern programming languages provide built-in functions or standard libraries for Base64 encoding and decoding. Here are some examples:

JavaScript (Browser & Node.js)

In modern browsers, you can use the `btoa()` (binary to ASCII) and `atob()` (ASCII to binary) functions. For Unicode strings, you need an extra step.

// Browser (for ASCII strings)
const string = 'Hello, World!';
const encoded = btoa(string); // "SGVsbG8sIFdvcmxkIQ=="
const decoded = atob(encoded); // "Hello, World!"

// To handle Unicode strings correctly
function toBase64(str) {
  return btoa(unescape(encodeURIComponent(str)));
}

function fromBase64(str) {
  return decodeURIComponent(escape(atob(str)));
}

// In Node.js
const string = 'Hello, World!';
const encoded = Buffer.from(string).toString('base64'); // "SGVsbG8sIFdvcmxkIQ=="
const decoded = Buffer.from(encoded, 'base64').toString('utf8'); // "Hello, World!"

Python

Python's standard library includes a `base64` module.

import base64

message = "Hello, World!"
message_bytes = message.encode('ascii')

base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii') # "SGVsbG8sIFdvcmxkIQ=="

message_bytes_decoded = base64.b64decode(base64_bytes)
message_decoded = message_bytes_decoded.decode('ascii') # "Hello, World!"

Java

Java 8 introduced a `Base64` class in the `java.util` package.

import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        String originalInput = "Hello, World!";
        
        String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
        // "SGVsbG8sIFdvcmxkIQ=="
        
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
        String decodedString = new String(decodedBytes);
        // "Hello, World!"
    }
}

C#

C# provides methods in the `System.Convert` class.

using System;
using System.Text;

public class Base64Example
{
    public static void Main(string[] args)
    {
        string originalString = "Hello, World!";
        var plainTextBytes = Encoding.UTF8.GetBytes(originalString);
        
        string encodedString = Convert.ToBase64String(plainTextBytes);
        // "SGVsbG8sIFdvcmxkIQ=="
        
        var base64EncodedBytes = Convert.FromBase64String(encodedString);
        string decodedString = Encoding.UTF8.GetString(base64EncodedBytes);
        // "Hello, World!"
    }
}