Blog / What is Base64 Encoding?

What is Base64 Encoding? A Developer's Guide

Published on caseconverter.co.uk· 6 min read

In the vast world of web development and data transmission, you have likely encountered strings of seemingly random characters like aGVsbG8gd29ybGQ=. This is not gibberish. It is an example of Base64 encoding, a fundamental concept for any developer to understand. 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 guide will demystify Base64 encoding, explaining what it is, how it works, where it is used, and when you should, and should not, use it.

How Does Base64 Encoding Work?

At its core, Base64 is a method for converting binary data, such as images or executables, into a format that can be reliably transmitted over systems that are designed to handle only text. Think of it as a universal language for representing binary data in a text-only environment.

The process might seem complex at first, but it can be broken down into a few logical steps. Let's take the simple string "man" and see how it gets encoded.

  1. Text to Binary: First, we convert each character of the string into its 8-bit binary representation using a character set like ASCII or UTF-8. For "man", the ASCII values are m = 109, a = 97, n = 110. Their binary representations are:
    • m: 01101101
    • a: 01100001
    • n: 01101110
  2. Concatenate and Reshape: We then concatenate these binary strings into a single 24-bit sequence: 011011010110000101101110.
  3. Split into 6-Bit Chunks: This 24-bit sequence is then divided into 6-bit chunks. Why 6 bits? Because 2^6 equals 64, which is the number of unique characters in the Base64 character set. Our sequence becomes:
    • 011011
    • 010110
    • 000101
    • 101110
  4. Map to Base64 Characters: Each 6-bit chunk is converted to its decimal equivalent, which then maps to a specific character in the Base64 index table. The standard Base64 character set includes A-Z, a-z, 0-9, and two special characters, + and /.
    • 011011 (decimal 27) → b
    • 010110 (decimal 22) → W
    • 000101 (decimal 5) → F
    • 101110 (decimal 46) → u

So, the Base64 encoding of "man" is "bWFu".

What About Padding?

What happens if the input data is not a multiple of three bytes (24 bits)? This is where padding comes in. Base64 requires that the input binary data be a multiple of 24 bits. If it is not, the input is padded with zero bits to make it a multiple of 24. Then, one or two padding characters, represented by the equals sign (=), are added to the end of the encoded output.

If the original data has one byte left over, it is padded to create two 6-bit chunks, and two = characters are added to the output. If there are two bytes left over, they are padded to create three 6-bit chunks, and one = character is added. This padding ensures that the encoded data can be correctly decoded back to its original binary form without any ambiguity. It signals to the decoder how many bytes were added to the original data during the encoding process.

Common Uses of Base64 Encoding

Base64 is not just a theoretical concept. It is used extensively in many applications you interact with daily. Its ability to safely transmit binary data through text-based channels makes it incredibly versatile.

Email Attachments

The original email protocol, SMTP (Simple Mail Transfer Protocol), was designed to handle only plain text. This posed a problem for sending files like images, documents, or videos. Base64 encoding provides the solution. Binary files are encoded into a Base64 string and included in the email body. The recipient's email client then decodes this string back into the original file. This is a core part of the MIME (Multipurpose Internet Mail Extensions) standard that governs modern email.

Data URIs

Data URIs (Uniform Resource Identifiers) allow you to embed files directly into your HTML, CSS, or JavaScript files instead of linking to them as external resources. This is particularly useful for small images or icons. For example, instead of <img src="/path/to/icon.png">, you could use a Data URI. You can easily convert your images using a Base64 Image Encoder to see this in action.

Web APIs and JSON

When building APIs that need to transfer binary data within a JSON payload, Base64 is the standard approach. JSON is a text-based format and cannot natively handle binary data. By encoding the binary data into a Base64 string, you can safely include it as a value for a JSON key. This is common in scenarios like uploading user avatars or sending files to a server via a RESTful API.

Storing Binary Data in XML or Databases

Similar to JSON, XML is a text-based format. If you need to store binary data, such as a small image or a digital signature, within an XML document, Base64 is the way to go. Some databases that are not optimised for binary large objects (BLOBs) might also use Base64 strings to store binary data in text fields.

When to Use (and Not Use) Base64

Base64 is a powerful tool, but it is not a one-size-fits-all solution. Knowing when to use it is just as important as knowing how it works.

Use Base64 When:

  • You need to transmit binary data over a text-only channel. This is the primary use case for Base64, as seen in email attachments and API responses.
  • You want to embed small resources directly into a file. Data URIs are a great example of this, helping to reduce HTTP requests for small icons and images.
  • You need to store binary data in a system that only supports text. This could be a database field or an XML attribute.

Do Not Use Base64 When:

  • You are concerned about data size. Base64 encoding increases the size of the data by approximately 33%. For large files, this overhead can be significant.
  • You need encryption. It is crucial to remember that Base64 is an encoding scheme, not an encryption algorithm. It provides no security and is easily reversible.
  • You are handling large files in a web context. For large images or videos, it is better to serve them as separate files. Embedding a large Base64-encoded file in your HTML will dramatically increase the page size.

Conclusion

Base64 encoding is a fundamental building block of the modern web, providing a reliable bridge between the binary world of files and the text-based world of data transmission. By understanding how it converts binary data into a safe, readable ASCII format, you can better appreciate its role in everything from your email inbox to the websites you visit. For a hands-on look at how text is converted to its binary form, you can explore a Text to Binary converter.