HomeDeveloper & IT › Base64 Size Calculator

Base64 Size Calculator

Computes the length of the Base64 encoding of a given number of bytes (4 characters per 3 bytes, padded with =), the 33 % overhead, the size with MIME/PEM line breaks and the length of a data: URI.

When to use

You need to know how large a file becomes when embedded as Base64 in JSON, email (MIME) or a data URI, or how many bytes a Base64 string decodes to.

Do not use when: You need to actually encode or decode data, or you are sizing Base32/Base16 output (8 characters per 5 bytes, 2 per byte).

Formula

encoded = 4 × ceil(size_bytes / 3); padding = (3 − size_bytes mod 3) mod 3; lines = ceil(encoded / line_length); with_breaks = encoded + 2 × lines; data_uri = encoded + length('data:' + mime_type + ';base64,')

RFC 4648 section 4 Base64 with = padding; MIME (RFC 2045) wraps at 76 characters with CRLF line endings, PEM at 64. Unpadded variants (base64url in JWTs) are shorter by padding_characters.

Inputs

ParameterTypeUnitRequiredDescription
size_bytesintegerbytesyesNumber of bytes before encoding. Range: ≥ 0, ≤ 1000000000000000
line_lengthintegercharactersdefault 0Characters per line when the output is wrapped (76 for MIME per RFC 2045, 64 for PEM); 0 = single line without breaks. Range: ≥ 0, ≤ 1000
mime_typestringdefault image/pngMedia type written into the data: URI prefix (data:<mime>;base64,).

Outputs

OutputTypeUnitDescription
encoded_charactersintegercharacters4 × ceil(size_bytes / 3) characters including padding.
padding_charactersintegerNumber of trailing = characters (0, 1 or 2).
overhead_percentnumber%(encoded − raw) / raw × 100; tends to 33.33 % for large inputs.
encoded_with_line_breaksintegercharactersEncoded length plus a CRLF (2 characters) after every line of line_length characters; equals encoded_characters when line_length = 0.
line_countintegerNumber of lines when wrapped (1 when not wrapped).
data_uri_charactersintegercharactersLength of data:<mime_type>;base64,<encoded> (no line breaks).
data_uri_prefixstringThe prefix that precedes the Base64 payload.
decoded_from_encodedstringHow to recover the byte count from an encoded length: 3 × encoded / 4 − padding.

Example

3 bytes: {"size_bytes":3,"line_length":0}{"encoded_characters":4,"padding_characters":0,"overhead_percent":33.33,"encoded_with_line_breaks":4,"data_uri_characters":26}

1,000,000 bytes with MIME line breaks: {"size_bytes":1000000,"line_length":76}{"encoded_characters":1333336,"padding_characters":2,"overhead_percent":33.33,"line_count":17544,"encoded_with_line_breaks":1368424,"decoded_from_encoded":"3 × 1333336 / 4 − 2 padding = 1000000 bytes"}

GET https://tttkmbb.com/api/v1/calculate/base64-size?size_bytes=3&line_length=0

Machine access

Sources

FAQ

Why is Base64 33 % larger?

Every 6 bits of input become one 8-bit ASCII character, so 3 bytes turn into 4 characters (4/3 = 1.333); padding adds up to 2 more characters at the end.

How many bytes does a Base64 string of length L decode to?

3 × L / 4 minus the number of trailing = characters (L must be a multiple of 4 when padded).

Does gzip undo the overhead?

Partly: Base64 text compresses to roughly the size of the compressed original plus a few percent, but only if the transport compresses it (e.g. HTTP content-encoding).

Related calculators