Base64 Length: Padding, Encoded Characters and Decoded Bytes
Published · Updated
For standard padded Base64, the encoded length is 4 × ceil(n / 3), where n is the number of input bytes. Count the bytes before Base64 encoding; a Unicode character count can give a different starting number.
Calculate the padded payload length
RFC 4648, section 4 groups three input bytes into four ASCII output characters. A final one-byte group ends with ==; a two-byte group ends with =. A complete three-byte group needs no padding. The formula rounds up to include that final group, and empty input produces empty output.
These exact examples encode the input text as UTF-8 first. Encoded length includes the equals signs, but excludes quotes, line wrapping and surrounding metadata. Because the output is ASCII, its character count equals its UTF-8 byte count.
| Input text | Input bytes | Base64 | Encoded length |
|---|---|---|---|
| A | 1 | QQ== | 4 |
| AB | 2 | QUI= | 4 |
| ABC | 3 | QUJD | 4 |
| 😀 | 4 | 8J+YgA== | 8 |
| é😀 | 6 | w6nwn5iA | 8 |
| (empty) | 0 | (empty) | 0 |
é😀 contains two Unicode code points and six UTF-8 bytes. Its Base64 form, w6nwn5iA, contains eight characters. Start from six bytes when applying the formula. The String Length guide explains the separate choice of counting unit.
Work back to decoded bytes
For a correctly formed padded payload, use n = 3 × (L / 4) − p. Here L includes the final padding and p is the number of final equals signs: zero, one or two. For QQ==, L = 4 and p = 2, so the decoded size is one byte.
This is a size calculation, not an input validator. A multiple-of-four length alone cannot establish valid Base64. Line breaks count as extra text, and a protocol that omits padding needs its own rules before this padded calculation applies.
Verify text, bytes and Base64 separately in Python
In Python’s text and binary types, encoding a string creates bytes. The base64 module then encodes those bytes and can decode them back. This small in-memory example keeps each stage:
import base64
text = "é😀"
data = text.encode("utf-8")
encoded = base64.b64encode(data).decode("ascii")
decoded = base64.b64decode(encoded, validate=True)
print(encoded)
print(len(text), len(data), len(encoded), len(decoded))
The output is w6nwn5iA, followed by 2 6 8 6: text code points, input bytes, encoded characters and decoded bytes. The returned bytes match the original UTF-8 input.
validate=True rejects non-alphabet characters such as line breaks. Handle decoding errors using the receiving protocol’s rules; the flag does not certify every protocol requirement or canonical encoding. Successful Base64 decoding also does not prove that the bytes are UTF-8 text. For example, /w== decodes to the single byte FF, which cannot stand alone as UTF-8.
Exclude the wrapper only when the rule does
A data URL has metadata before its comma. In data:text/plain;base64,QQ==, the bare Base64 payload has four characters, while the complete URL includes the prefix too. Name which representation the receiving limit covers.
A JSON field adds another representation around the Base64 text. Use the JSON length guide when the rule covers that serialized document. Neither the bare encoded length nor decoded byte count establishes an upload service’s limit or includes transport overhead.