How-toMath & conversion4 min read

How Base64 Encoding Works (Step by Step, With Examples)

What Base64 is, how three bytes become four characters, and why padding exists.

SH

Founder, CalculatePilot

On this pageexpand_more
How Base64 encoding works
lightbulb

Short answer

Base64 splits data into 6-bit groups and maps each to one of 64 characters, so every 3 bytes become 4 characters and the output is about 33% larger. 'Man' (4D 61 6E) becomes TWFu; 'Ma' becomes TWE= with one = of padding.

1. The idea

Base64 is a way to write binary data using only 64 safe, printable characters: A to Z, a to z, 0 to 9, + and /. It lets bytes travel through systems built for text.

2. Worked example: Man

  1. 1Take the bytes: M = 4D, a = 61, n = 6E.
  2. 2Write the 24 bits: 010011 010110 000101 101110.
  3. 3Look up each 6-bit group (19, 22, 5, 46) in the alphabet: T, W, F, u.
  4. 4Result: TWFu.

3. Padding

InputBytesBase64
Man3TWFu
Ma2TWE=
M1TQ==

When the input is not a multiple of 3 bytes, = characters fill the final group of four.

4. Size

Length = 4 × ceil(bytes ÷ 3). A 22-byte input gives 32 characters (2 of them padding).

5. Not encryption

Anyone can decode Base64. It is only a change of alphabet.

Try it in the Base64 Encode / Decode tool. See also why Base64 decoding fails.

6. Frequently asked questions

What is Base64?expand_more

A binary-to-text encoding that writes data with 64 printable characters. Every 3 bytes become 4 characters.

Why does Base64 end with =?expand_more

Padding. It fills the last group of 4 characters when the input is not a multiple of 3 bytes.

How much bigger is Base64?expand_more

About 33% bigger: 4 characters for every 3 bytes.

Is Base64 secure?expand_more

No. It is an encoding that anyone can reverse, not encryption.

7. Sources

Spotted a mistake or a number that looks off?

We fix errors quickly and note the change.