Skip to content
Padding
step 1/5

Reading — step 1 of 5

Read

~1 min readEncoding

Padding

The = character pads the output to a multiple of 4 chars:

  • 0 bytes left over: no padding
  • 1 byte left over: 2 = chars
  • 2 bytes left over: 1 = char

Example:

3 bytes   "abc"   ->  "YWJj"      (4 chars, no pad)
4 bytes   "abcd"  ->  "YWJjZA=="  (8 chars, 2 pad)
5 bytes   "abcde" ->  "YWJjZGU="  (8 chars, 1 pad)
6 bytes   "abcdef"->  "YWJjZGVm"  (8 chars, no pad)

Padding makes the output length always a multiple of 4. This lets parsers know exactly where to stop reading.

Some variants (base64url) DROP the padding to save 1-2 chars in URLs. Decoders must accept both forms — the trailing bytes are derivable from the length:

text length % 4 == 0:   no padding
text length % 4 == 2:   add ==
text length % 4 == 3:   add =
text length % 4 == 1:   ERROR (impossible for valid base64)

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…