Skip to content

Why File Size Increases by 33% with Base64 Encoding

Category: Encoding

Have you ever heard that 「file size increases by about 33% when encoded in Base64」? Why is this? This article explains how Base64 encoding works, the mathematical reasons for file size increase, and its impact in development environments.

What is Base64

Base64 is an encoding scheme that represents binary data using only ASCII characters. It is used for transmitting binary data over text-only protocols, such as email attachments (MIME), data URI schemes, JWT tokens, and Basic authentication headers.

The characters used are A–Z (26 characters), a–z (26 characters), 0–9 (10 characters), +, / for a total of 64 characters (= Base64), plus = for padding.

Why does it increase by 33%

Base64 converts 3 bytes of binary into 4 ASCII characters.

  • 3 bytes = 24 bits
  • 24 bits ÷ 6 bits = 4 Base64 characters (each character represents 6 bits)

In other words, the original data becomes 3 bytes → 4 characters. The size ratio is 4 ÷ 3 ≈ 1.333..., so it increases by approximately 33.3%.

元バイナリ:   | 0x4D | 0x61 | 0x6E |  ← 3バイト
              01001101 01100001 01101110
              ↓ 6ビットずつに分割
              010011 010110 000101 101110
              ↓ Base64文字に変換
                 T       W       F       u   ← 4文字(= 4バイト)

Calculation of concrete size increase

Original file sizeSize After Base64Increase amount
1 MB(1,000,000 B)Approximately 1.333 MB+333 KB
10 MBApproximately 13.33 MB+3.33 MB
25 MB (Gmail limit)Approximately 33.3 MB+8.33 MB
100 MBApproximately 133.3 MB+33.3 MB

The exact formula is as follows.

import math

def base64_size(original_bytes: int) -> int:
    """Base64エンコード後のバイト数を計算"""
    # 3バイトを4文字に変換、4の倍数にパディング
    return math.ceil(original_bytes / 3) * 4

original = 10_000_000  # 10 MB
encoded  = base64_size(original)
print(f"元: {original:,} B")
print(f"後: {encoded:,} B")
print(f"増加率: {encoded/original*100:.1f}%")
# → 元: 10,000,000 B
# → 後: 13,333,336 B
# → 増加率: 133.3%

Additional increase due to line break characters

The MIME standard (email) requires that Base64 data be broken into lines every 76 characters with \r\n (CRLF). These line breaks also add extra bytes, slightly increasing the overall size.

def base64_mime_size(original_bytes: int, line_length: int = 76) -> int:
    """MIME形式(76文字改行)のBase64サイズを計算"""
    b64_chars = math.ceil(original_bytes / 3) * 4
    line_count = math.ceil(b64_chars / line_length)
    return b64_chars + line_count * 2  # CRLF = 2バイト/行

Development impact

File Transfer via API

When sending a file included in a JSON body via REST API, Base64 encoding is required.

// ファイルをBase64に変換してJSONで送る
const file = document.getElementById('file').files[0];
const reader = new FileReader();
reader.onload = (e) => {
    const base64 = e.target.result; // "data:image/png;base64,iVBORw0KGgo..."
    fetch('/api/upload', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ file: base64, name: file.name })
    });
};

In this case, the API request size limit is determined by the size after Base64 encoding. Note that when sending a 10MB file, the JSON body will be approximately 13.3MB or larger.

Email Attachment (SMTP)

Because the SMTP protocol is ASCII text-based, attachments are Base64-encoded. Gmail's attachment limit is 25 MB, which refers to the file size before encoding (Gmail handles the internal encoding). However, when sending directly via SMTP, the size limit applies to the encoded size, so caution is required.

Data URI for img Tag

<!-- Base64埋め込み画像 -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." />

When embedding images inline in HTML, the HTML file becomes larger by the Base64 size of the image. It is generally recommended to avoid inline embedding of large images as it affects page load speed.

Decode Base64 and Restore to Original Size

Summary

  • Base64 converts 3 bytes → 4 characters, causing size to increase by approximately 33.3%
  • Further Slight Increase When MIME Line Breaks (Every 76 Characters) Are Present
  • For APIs That Send Files via JSON Body, the Request Limit is Determined by Post-Base64 Size
  • Base64 encoding is not required for HTML forms with multipart/form-data (binary is sent as-is)

Test files for this article

❓ Frequently Asked Questions

Why does Base64 encoding increase file size by 33%?
Base64 maps every three bytes of binary onto four ASCII characters, a ratio of 4/3 ≈ 1.333, so the data grows by about 33.3%. The MIME variant adds a CRLF every 76 characters, pushing it slightly higher.
What size limits matter when sending Base64-encoded files through an API?
The API's request size limit applies to the encoded payload. Sending a 10 MB file as JSON produces a body of at least about 13.3 MB. An HTML form using multipart/form-data sends the binary as-is with no Base64 step, so the overhead stays in the hundreds of bytes.
How do I calculate the size of a file after Base64 encoding?
Divide the original byte count by three, round up, then multiply by four. A 10,000,000-byte (10 MB) file becomes 13,333,336 bytes, about 13.3 MB. In MIME format add two more bytes for the CRLF after every 76 characters.