Base64 encoding: what it does, what it doesn't do, and when to use it

Why we convert binary files into long strings of text. Learn the 64-character alphabet and the 33% overhead rule.

Base64 is an encoding scheme, not a compression or encryption algorithm. Its sole purpose is to take raw binary data (like an image or a compiled program) and translate it into a safe, printable string of ASCII text. It uses an alphabet of 64 characters: A-Z, a-z, 0-9, plus '+' and '/'.

This is necessary because many legacy data transport systems, such as email (SMTP) or JSON data payloads, were designed strictly to handle text. If you inject raw binary bytes into JSON, the parser will hit invisible control characters (like 'null' or 'end of file') and crash.

The mechanics of the Base64 conversion

Binary data is organized in 8-bit bytes. Base64 works by taking a group of three 8-bit bytes (24 bits total) and splitting them into four 6-bit chunks. Because a 6-bit chunk has exactly 64 possible values (2^6), each chunk maps perfectly to one of the 64 safe ASCII characters.

If the total number of bytes is not cleanly divisible by three, Base64 uses the '=' character as padding at the end of the string. Seeing '==' at the end of a string is a nearly universal visual indicator that you are looking at Base64 encoded data.

The 33% overhead penalty

Because Base64 takes 3 bytes of raw data and expands them into 4 bytes of text (since every ASCII character requires a full 8-bit byte to store), the encoding inherently increases the file size by exactly 33%.

Embedding a 3MB image directly into a CSS or HTML file using a Base64 data URI sounds efficient because it saves an HTTP request. However, that image will now consume 4MB of text within your codebase. For small icons, this is acceptable; for large files, the 33% bandwidth penalty severely degrades web performance.