How Base64 encoding works: the algorithm that turns binary into text step by step
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.
By TechCompare · Updated
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
The algorithm reads three input bytes (24 bits), slices them into four 6-bit groups, and maps each group onto the 64-character alphabet. Tail bytes drive the padding: one leftover byte produces two characters plus ==, two leftover produce three plus =. Lengths are always multiples of four, which is how URL-safe variants know to strip padding. Reversibility is guaranteed because the math is deterministic.
