DocsME
6 min readDocsMe Team

How Base64 Encoding Works

See exactly how Base64 turns bytes into text — the 3-byte to 4-character mapping, the alphabet, padding, and why the output is about 33% larger.

  • how base64 encoding works
  • base64 algorithm
  • base64 padding
  • base64 alphabet
  • pdf me

From Bytes to Base64 Characters

Base64 works on the original file 3 bytes — 24 bits — at a time. Each 24-bit group is split into four 6-bit chunks, and each 6-bit chunk (a number from 0 to 63) is looked up in a fixed alphabet to produce one output character. Three bytes in, four characters out.

This is why every Base64 string is built from the same 64 symbols, no matter what kind of file — a PDF, an image, a font — went in. See what Base64 encoding is for the bigger picture.

The Base64 Alphabet

The 64 possible 6-bit values map to: the uppercase letters A–Z (values 0–25), the lowercase letters a–z (values 26–51), the digits 0–9 (values 52–61), and finally + and / (values 62 and 63). A handful of variants swap the last two symbols — URL-safe Base64 uses - and _ instead, so the string can sit safely inside a URL.

Padding With =

A 24-bit group only forms cleanly when the input length is a multiple of 3 bytes. When a file's last group has only 1 or 2 bytes left over, Base64 pads the missing bits with zeros and appends one or two = characters to the output so decoders know exactly how many of the final bits were real data.

Padding does not carry any information from the file — it only marks where real content ends, so a decoder can stop at the right place.

A Small Worked Example

Encoding the two letters "Hi" produces the string "SGk=". The bytes for H and i form 16 bits, which split into three full 6-bit chunks plus 2 leftover bits padded with zeros — three real characters (S, G, k) followed by one = padding character, because the input was 2 bytes, not a multiple of 3.

Why the Output Is About 33% Larger

Three bytes of binary data always become four characters of text, a 4:3 ratio — about 33% bigger. A PDF that is 900 KB on disk becomes a Base64 string close to 1.2 MB. This is the main tradeoff to plan around; see the size guidance in the PDF to Base64 guide.