Base64 Converter

A versatile tool to encode text or files into Base64 and decode Base64 strings back to text or files.

Encode Text or File to Base64
OR

What is Base64?

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. It is commonly used to encode data that needs to be stored and transferred over media that are designed to deal with text. This ensures that the data remains intact without modification during transport.

The character set used by Base64 consists of 64 characters: 26 uppercase letters (A-Z), 26 lowercase letters (a-z), 10 digits (0-9), and two additional characters, typically '+' and '/'. A padding character, '=', is also used at the end of the encoded data if necessary.

How Base64 Encoding Works

  1. 1. The input data (e.g., text) is first converted into a sequence of bytes.
  2. 2. These bytes are then grouped into blocks of 24 bits (3 bytes).
  3. 3. Each 24-bit block is divided into four 6-bit groups.
  4. 4. Each 6-bit group is mapped to one of the 64 characters in the Base64 character set. This character becomes part of the output string.
  5. 5. If the last block of bytes has fewer than 24 bits, padding (`=`) is added to the end of the output string to ensure its length is a multiple of 4.

Common Uses of Base64

  • Email Attachments: Encoding binary files like images or documents for embedding in email bodies.
  • Data URIs: Embedding images or other files directly into HTML or CSS files using the `data:` scheme, reducing the number of HTTP requests.
  • Basic HTTP Authentication: Encoding usernames and passwords for the `Authorization` header.
  • Storing Binary Data: Storing binary data in text-based formats like XML or JSON where raw binary is not supported.

Base64 in Different Programming Languages

Here's how you can perform Base64 encoding and decoding in various popular languages:

JavaScript

Encode:

btoa("your string");

Decode:

atob("eW91ciBzdHJpbmc=");

Python

Encode:

import base64
base64.b64encode(b'your string')

Decode:

import base64
base64.b64decode(b'eW91ciBzdHJpbmc=')

Java

Encode:

import java.util.Base64;
String encodedString = Base64.getEncoder().encodeToString("your string".getBytes());

Decode:

import java.util.Base64;
byte[] decodedBytes = Base64.getDecoder().decode("eW91ciBzdHJpbmc=");
String decodedString = new String(decodedBytes);

PHP

Encode:

base64_encode('your string');

Decode:

base64_decode('eW91ciBzdHJpbmc=');

C#

Encode:

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes("your string");
string encodedString = System.Convert.ToBase64String(plainTextBytes);

Decode:

var base64EncodedBytes = System.Convert.FromBase64String("eW91ciBzdHJpbmc=");
string decodedString = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);