How Base64 turns binary into text, step by step

By TechCompare · Updated · How we check

Base64 encoding is a straightforward 3-to-4 byte expansion with optional padding. Understanding the algorithm helps you debug encoding issues and explains why Base64 output is always a multiple of 4 characters. For practical use, rely on your language's standard library rather than implementing it yourself.

Base64 encoding takes arbitrary binary data and produces text using a 64-character alphabet (A-Z, a-z, 0-9, +, /). The algorithm processes input 3 bytes (24 bits) at a time, splits them into four 6-bit groups, and maps each group to a character in the alphabet. Every 3 bytes of input produce exactly 4 characters of output.

Encoding focus
How Base64 works
base64-algorithm
Category
Fundamentals
Encoding concepts and theory

How this is calculated

When the input isn't a multiple of 3 bytes, Base64 adds padding. One extra byte produces two Base64 characters plus == padding. Two extra bytes produce three Base64 characters plus = padding. The = characters signal to the decoder how many padding bytes to strip.

Some variants (URL-safe Base64, AWS Signature V4) omit padding entirely because the length of the encoded string already implies the original length. The algorithm is deterministic and reversible: the same input always produces the same output, and decoding always recovers the original bytes.

Verdict

Use your language's standard library, because the algorithm is fixed and reversible. Every 3 input bytes become exactly 4 characters, and = padding fills any leftover tail so the output length is always a multiple of four.

More Encoding scenarios

Base64 vs Hex
Base64 and hexadecimal both encode binary data as text, but they have different purposes.
View details
UTF-8 vs ASCII
ASCII maps 128 English characters to 7-bit values.
View details
URL encoding guide
Percent-encoding (also called URL encoding) represents a byte with a percent sign followed by two hex digits when that byte can't appear literally in the URL component being built.
View details

Frequently asked questions

How does Base64 turn binary data into text?
It reads three input bytes (24 bits) at a time, slices them into four 6-bit groups, and maps each group to one character from a 64-character alphabet (A-Z, a-z, 0-9, +, /). Every 3 bytes of input become exactly 4 characters of output, which is why Base64 is one-third larger than the data it carries.
Why does Base64 output end with = or ==?
Padding for tails that aren't a multiple of 3 bytes. One leftover byte produces two characters plus ==, and two leftover bytes produce three characters plus =. The equals signs tell the decoder how much of the final quartet is real data. URL-safe variants drop the padding because the string length already implies it.
Is Base64 encryption or encoding?
Encoding. It's a fully public, reversible algorithm with no key, so it hides nothing from anyone who recognizes it, and every language has a decoder built in. Base64 exists to move binary through text-only channels like JSON, email, and URLs. If the content is sensitive, it needs real encryption before the Base64 step.