← All blog posts

Base64 formula explained simply

WhiteUI · A plain-English guide to the Base64 Encoder Decoder

Base64 has no long formula to memorise. The whole method fits in two short rules: three bytes become four characters, and the output is padded to a multiple of four. This guide explains both rules, shows the one formula that predicts the output length before you encode anything, and works through the padding table that trips most people up. Check every number here on the Base64 Encoder Decoder, which shows the encoded result for anything you type.

One byte becomes four characters with two equals signs, two bytes with one, three bytes with none
Three bytes in, four characters out, always: 1 byte gives 4 characters with ==, 2 bytes with =, 3 bytes with no padding.

The first rule: three bytes become four characters

Every byte is 8 bits, so three bytes are 24 bits. Base64 splits those 24 bits into four groups of 6 bits, and each group of 6 bits is one character, because 6 bits can hold the numbers 0 to 63 and the alphabet has exactly 64 characters. That is why the output always grows: three bytes of input produce four characters of output. The ratio never changes, whatever the text is. A word, a picture and a password all follow the same rule, which is why you can predict the size of the result before you run it.

The formula for the output length

To find how long the encoded text will be, divide the number of bytes by three, round up to a whole number of groups, and multiply by four. Written as a formula, it is output length = 4 times the ceiling of bytes divided by 3. For Hello, which is 5 bytes: 5 divided by 3 is 1.67, rounded up to 2 groups, times 4 gives 8 characters, which matches SGVsbG8=. For a 100-byte message: 100 divided by 3 is 33.33, rounded up to 34 groups, times 4 gives 136 characters. For 1,000 bytes the answer is 1,336 characters, and for a 10-kilobyte file it is 13,656 characters.

output length = 4 × ceil(bytes ÷ 3)
overhead = output length − bytes

The padding table

When the bytes do not divide evenly into threes, the last group is filled with zero bits and marked with equals signs. One leftover byte produces two real characters and two equals signs: M becomes TQ==. Two leftover bytes produce three real characters and one equals sign: Ma becomes TWE=. Three bytes fill the group exactly, so there is no padding: Man is simply TWFu. The pattern repeats every three bytes, so a four-byte input such as ABCD behaves like one byte left over after the first group: it encodes to QUJDRA==, which is 8 characters with two equals signs at the end. Count the equals signs and you know how many bytes the last group held.

Why the output is always a multiple of four

The equals signs are not just tidiness; they are how the decoder knows where the data ends. A Base64 string with its padding removed is ambiguous: the decoder cannot tell whether the last group held one byte or two. Keeping the length a multiple of four removes that doubt, because every group of four characters stands for exactly three bytes, minus the padding. Some systems strip the padding to save space, but then the decoder has to guess the length from the context, and guessing is where errors creep in. Keep the padding unless the system you are feeding explicitly says it does not want it.

The size cost, in one number

The formula also tells you the cost. Three bytes become four characters, so the output is one third bigger than the input, plus at most two padding characters. On long text that settles at 33%: 100 bytes become 136 characters, which is 36% bigger only because the padding is a bigger share of a short string, while 1,000 bytes become 1,336, which is 33.6% bigger, and 10 kilobytes become 13,656, which is 33.4% bigger. The longer the input, the closer the overhead gets to exactly one third. That is the whole trade: Base64 buys safe passage through text-only channels for a third more characters, and the formula tells you the price before you pay it.

Frequently asked questions

What is the Base64 formula?

Output length = 4 times the ceiling of bytes divided by 3. Three bytes become four characters, and = signs pad the end to a multiple of four.

How long is the Base64 of 100 bytes?

136 characters. 100 divided by 3 is 33.33, rounded up to 34 groups, times 4 gives 136.

What does == at the end of Base64 mean?

The last group held one byte. One = means it held two bytes, and no padding means the bytes divided exactly into threes.

Why is Base64 output always a multiple of 4?

Every group of four characters stands for three bytes, and the padding fills the last group so the decoder knows exactly how many bytes it held.

More guides on the Base64 Encoder Decoder

Try it yourself

Try the Base64 Encoder Decoder — free, in your browser.

Open the Base64 Encoder Decoder