Checking padded Base64 size before copying a payload
Padded Base64 turns every group of up to three bytes into four characters, so the encoded length jumps in steps instead of growing one character at a time. This guide shows how to predict that length before you copy a payload.
The size question behind a Base64 field
People usually ask about Base64 length when a field has a limit. An HTTP header, a JSON string in a message queue, a URL parameter, a database column or a form input can reject a value for being too long, and the value being pasted is often an encoded copy of something smaller. Knowing the encoded length in advance saves a failed request. The rule for padded Base64, as defined in RFC 4648, is short: the encoder reads the input three bytes at a time and writes four characters for each group, padding the final group with equals signs when it holds fewer than three bytes. That means the output length is 4 times the ceiling of n divided by 3, where n is the number of source bytes. The rest of this guide explains where that formula comes from, why the length is based on bytes, and how to confirm it with the worked cases.
Why three bytes become four characters
A byte has 8 bits, so three bytes carry 24 bits. Base64 uses an alphabet of 64 symbols, and each symbol represents exactly 6 bits. Twenty-four bits split into four chunks of 6 bits, so three bytes fit evenly into four characters with no leftover. That is the whole design: the group size of three is the smallest number of bytes that divides into whole 6-bit symbols. The 64 symbols in the standard alphabet are the uppercase letters, the lowercase letters, the digits 0 to 9, plus and slash, and the equals sign is reserved for padding rather than data. Because 4 characters carry 3 bytes, the encoded text is about one third larger than the source, a ratio of 4 to 3, which is roughly 33 percent overhead before any line breaks are added.
Comparing foo and foob
The worked cases use two adjacent inputs to show the stepping behavior. The text foo is three ASCII bytes, so it fills exactly one group and encodes as Zm9v, which is four characters with no padding. Add the letter b and the input becomes four bytes. The first three bytes still produce Zm9v. The fourth byte starts a second group that holds only one byte, which is 8 bits, enough for two 6-bit symbols with 4 bits of zero fill. The encoder writes those two characters and then two equals signs to complete the four-character block. The result is Zm9vYg==, eight characters. One extra input byte added four output characters. The length did not grow gradually from 4 to 5. It jumped from 4 to 8, and it will stay at 8 until the input reaches seven bytes.
What padding means for one and two leftover bytes
The padding depends on how many bytes remain in the last group. If the input length is a multiple of three, there is no padding. If one byte is left over, the output ends with two equals signs, as in Zm9vYg==. If two bytes are left over, the output ends with a single equals sign. The standard test vectors in RFC 4648 show the sequence nicely: f encodes as Zg==, fo as Zm8=, foo as Zm9v, foob as Zm9vYg==, fooba as Zm9vYmE= and foobar as Zm9vYmFy. Lengths for those inputs are 4, 4, 4, 8, 8 and 8 characters. Each three-byte boundary adds four characters, and the padding characters exist so that the decoder can tell how many real bytes the last group contained. They carry no data, but they do count toward a length limit.
Count bytes, not characters you can see
The input to the length formula is a byte count, and a byte count is not the same as the number of characters a person sees. Plain ASCII text uses one byte per character, so foo has three bytes. Most other text does not. In UTF-8, accented Latin letters such as é take two bytes, the euro sign takes three bytes, and many emoji take four. The single character é encodes as w6k=, because its two bytes C3 and A9 form a short group, and the euro sign encodes as 4oKs from its three bytes. A string of ten visible characters could therefore be anywhere from 10 bytes to 40 bytes before encoding. Use the UTF-8 Byte Length Calculator to get the real byte count first, then apply the formula. That is why the worked cases include a byte count check for foob, which confirms the 4 bytes behind the 8-character output.
A larger example with a size limit
Suppose you plan to embed a small image as a Base64 data value, and the file is exactly 1,000,000 bytes. Divide by 3 and round up: 333,333.33 becomes 333,334 groups. Multiply by 4 and the encoded length is 1,333,336 characters. The file overhead of 333,336 characters is a little over a third of the original. If the receiving system allows at most 1,200,000 characters, the image will not fit, and you can work backward to find the largest usable source size. A limit of 1,200,000 characters allows 300,000 groups, which is 900,000 bytes. Anything larger than that fails. These numbers are illustrative sizes, not measurements of a specific file, but the same two steps work for any payload: ceiling of bytes over 3, then multiply by 4.
Unpadded and URL-safe variants
RFC 4648 also defines a URL and filename safe alphabet, which replaces plus and slash with hyphen and underscore, and many systems drop the trailing equals signs altogether. JSON Web Tokens, for example, use the URL-safe alphabet without padding. Dropping padding shortens the length. With no padding, the output length for n bytes is the ceiling of 4n divided by 3. For 4 source bytes that gives 6 characters instead of 8. The padded formula and the unpadded formula agree whenever the input is a multiple of three bytes, and differ by one or two characters otherwise. When you compare lengths between two systems, write down which variant each one uses. A value that fits as unpadded might fail once a library adds the missing equals signs, and a decoder that insists on padding may reject an unpadded string.
Line breaks in older formats
Some formats wrap Base64 at a fixed width. MIME email, defined in RFC 2045, limits encoded lines to 76 characters, and PEM text for certificates and keys commonly uses 64-character lines. Each full line of 76 characters carries 57 source bytes, and each line ends with a line break that is also part of the transmitted text. Those breaks are not part of the Base64 alphabet and a strict decoder may ignore or reject them depending on its rules. When you estimate the size of a wrapped value, compute the unwrapped length first, then add the line breaks. For 1,333,336 characters wrapped at 76, there are 17,544 lines, and the line breaks add more characters. The worked cases ignore wrapping, because they measure the raw encoding of short strings.
Decoding checks and mistakes to avoid
A padded string should have a length that is a multiple of four. If it is not, the string is truncated, has been altered or uses unpadded form. Strict decoders also check that the unused bits in the last symbol are zero, which is why Zg== is valid but a variant such as Zh== is not, even though both would produce the same first byte in a lenient decoder. A typical mistake is to measure the encoded string with a character counter and treat the answer as the source size. The source is about three quarters as long. Another is encoding text that was already encoded, which multiplies the overhead. If a payload seems too large, check whether it went through Base64 twice, and compare the count of bytes before and after with the formula used here.
Working backward from an encoded string
Sometimes you hold the encoded text and need the size of the original. Count the characters, divide by 4 and multiply by 3, then subtract one byte for each trailing equals sign. For Zm9vYg== the length is 8, which gives 6 bytes, and the two equals signs reduce that to 4, the size of foob. The large illustrative value of 1,333,336 characters divides into 333,334 groups, which hold 1,000,002 bytes, and two equals signs bring it back to 1,000,000. With no padding sign the last group is full. This reverse calculation is useful when you inspect a log or a database column and want to know how big the stored file is. If the character count is not a multiple of 4 and the string is supposed to be padded, treat that as a sign of truncation, because the formula would otherwise give a fractional number of bytes.
Common places where Base64 appears
Base64 turns binary data into text that survives channels designed for text. Email attachments use it under MIME. Data URIs embed small images directly in HTML or CSS, with the encoded characters following a prefix that names the media type. HTTP Basic authentication, described in RFC 7617, sends the username and password joined by a colon and encoded in Base64, which is why a header that looks scrambled can be reversed by anyone. That last example shows a common misunderstanding. Base64 is an encoding, so it offers no secrecy, and any decoder reads it. The format also costs about a third more space than the original, so it is a poor choice for large files when a binary transfer is available. For short values such as tokens and keys the overhead is small and the convenience of plain text usually outweighs it.
Reproduce the checks
Three-byte source
Open the Base64 Encoder Decoder and enter:
- Text or encoded input
foo- Calculation mode
encode
Expected output:
Text
Zm9vThe source fills one complete group.
Four-byte source
Open the Base64 Encoder Decoder and enter:
- Text or encoded input
foob- Calculation mode
encode
Expected output:
Text
Zm9vYg==A new partial group adds four padded characters.
Source byte check
Open the Utf8 Byte Length Calculator and enter:
- Text or encoded input
foob
Expected output:
Bytes
4The size rule uses source bytes.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08
- Primary definition and convention reference ↗ · 2026-10-08