The encoder takes the input three bytes (24 bits) at a time and splits those 24 bits into four
6-bit groups. Each group is a number from 0 to 63, which indexes into the alphabet
A–Z, a–z, 0–9, +, /. Here
is the classic example, the ASCII string Man:
| Step | Value | |||
|---|---|---|---|---|
| Characters | M · a · n | |||
| Bytes | 77 · 97 · 110 | |||
| Bits | 01001101 01100001 01101110 | |||
| 6-bit groups | 010011 | 010110 | 000101 | 101110 |
| Index | 19 | 22 | 5 | 46 |
| Base64 | T | W | F | u |
So Man encodes to TWFu. When the input length is not a multiple of
three, the last group is padded with zero bits and the output is filled out with
= so it is still a multiple of four characters: Ma becomes
TWE= and M becomes TQ==. One = means the
final group held two bytes, two mean it held one.
Because every 3 bytes turn into 4 characters, Base64 output is about 33% larger than the input (4/3 of the size, rounded up to a whole group). MIME email adds a line break every 76 characters on top of that, which is what the Wrap at 76 option produces.