The core concept of the Base64 algorithm is grouping input data at the bit level. While standard bytes are 8 bits wide (values 0–255), Base64 uses 6-bit units (26 = 64 distinct values, 0–63).
The 24-Bit Common Multiple
The least common multiple of 8 (a byte) and 6 (a Base64 unit) is 24 bits:
- Input: 3 bytes × 8 bits = 24 bits
- Output: 4 characters × 6 bits = 24 bits
Whenever you have 3 bytes of raw binary data, Base64 maps them cleanly into exactly 4 printable ASCII characters without leftover bits.
Visual Bit-Splitting Example: "Man"
Let's walk through how the 3-letter ASCII word Man is converted:
| Step | Byte 1 ('M') | Byte 2 ('a') | Byte 3 ('n') | |||||
|---|---|---|---|---|---|---|---|---|
| ASCII Char | M |
a |
n |
|||||
| ASCII Dec | 77 | 97 | 110 | |||||
| 8-Bit Binary | 01001101 |
01100001 |
01101110 |
|||||
| 24-Bit Stream | 010011010110000101101110 |
|||||||
| 6-Bit Groups | 010011 |
010110 |
000101 |
101110 |
||||
| Decimal Index | 19 | 22 | 5 | 46 | ||||
| Base64 Char | T | W | F | u | ||||
Thus, the string Man encodes to TWFu.