Calculate length from three-byte groups and padding
RFC 4648 standard Base64 represents three input bytes as four output characters. A final one-byte group needs == padding; two bytes need =; three need none. With padding and no added line breaks, output length is 4 * Math.ceil(n / 3), where n is the input byte count. Zero bytes give length zero.
n = new TextEncoder().encode(input).length
length = 4 * Math.ceil(n / 3)
f 1 Zg== 4
fo 2 Zm8= 4
foo 3 Zm9v 4
foob 4 Zm9vYg== 8These fixed examples were run through the actual function. Inputs f, fo and foo all produce four characters despite different input lengths; their padding differs. Four-byte foob produces eight characters. Padding marks the final group’s format, not extra characters in the original text.
Distinguish characters, bytes and line breaks
Moyoutil uses TextEncoder to turn input into UTF-8 bytes. The example 가 uses three bytes and becomes 6rCA: four characters. An f followed by an LF line break uses two bytes and becomes Zgo=. Spaces and line breaks in the source are data when encoding. HTTP headers, JSON wrappers and data URL prefixes are outside this length formula.
base64("가") // 6rCA
base64("f\n") // Zgo=Enter source text in the Base64 tool and select Encode. For decoding, enter only the standard Base64 output. Decoding removes whitespace from its input; encoding preserves source whitespace. Base64URL - and _ and arbitrary binary file recovery are unsupported. /w== can be read as Base64 but produces an error because its bytes are not UTF-8 text. Editing input clears the old result and disables copying; convert again.
Frequently Asked Questions
Does Base64 always add about 33% to the size?
For large inputs the ratio approaches 4/3, but padding has a large effect on small inputs. One byte becomes four characters. Count input bytes and check whether padding is included to calculate the exact length.
Does the tool convert empty input to zero characters?
The formula returns zero for zero bytes, but Moyoutil’s interface asks you to enter content when the input is empty. The formula’s boundary case differs from the interface’s input policy.