CCalclabhub

Text & Writing

Padding

A final group of one byte takes two equals signs, a group of two takes one, and a full group takes none.

Last updated

The Formula

padding = (3 - (bytes mod 3)) mod 3 equals signs

A final group of one byte takes two equals signs, a group of two takes one, and a full group takes none.

SymbolMeaning
nInput bytes
pPadding

What Each Symbol Means

nbytes

Input bytes

The size of the data in bytes.

pcharacters

Padding

How many equals signs the output ends with.

See It Worked With Real Numbers

A formula on its own does not tell you which number goes where. The how to encode text to base64 page applies this equation to a concrete case, shows every intermediate step, and names the mistakes that produce a wrong answer even when the algebra is right.

Where This Formula Appears

This equation is the working core of How To Encode Text To Base64, filed under Everyday Life. Every number the guide produces comes from substituting into this one line.

Common Questions

What is the formula for Padding?

The formula for Padding is padding = (3 - (bytes mod 3)) mod 3 equals signs. A final group of one byte takes two equals signs, a group of two takes one, and a full group takes none.

What units should I use with Padding?

Substitute n in bytes, p in characters. Convert every input to those units before you start, because the equation only returns the right answer when the units on both sides agree.

What is a common mistake when using Padding?

Encoding characters instead of bytes. Base64 is defined over bytes. Encoding text without first converting it to UTF-8 gives the wrong result the moment a character needs more than one byte.