Base64 Encode / Decode

Convert text to or from Base64.

How to Use the Base64 Calculator

  1. **Choose your operation:** Select 'Encode' to convert plain text to Base64, or 'Decode' to convert a Base64 string back to plain text.
  2. **Paste or type your input:** Enter the text you want to encode (e.g., `Hello`) or the Base64 string you want to decode (e.g., `SGVsbG8=`) into the input field.
  3. **Click Calculate:** Press the Encode or Decode button to process your input instantly.
  4. **Copy the result:** The output appears below the button. Use the Copy button to copy it to your clipboard.
  5. **Verify padding (for decoding):** If you receive an error, check that your Base64 string includes the correct '=' padding characters and contains only valid Base64 characters.

How Base64 Encoding Works

Output length = ⌈n / 3⌉ × 4

Base64 works by splitting the input bytes into groups of 3 bytes (24 bits), then dividing each 24-bit group into four 6-bit chunks. Each 6-bit value (0–63) is mapped to one of the 64 characters in the Base64 alphabet. If the input length is not divisible by 3, padding characters (=) are appended to make the output length a multiple of 4.

Output length formula:

For an input of n bytes, the Base64-encoded output length is ⌈n / 3⌉ × 4 characters (including padding).

  • n — The number of bytes (characters) in the original input string.
  • ⌈ ⌉ — The ceiling function — rounds up to the nearest whole integer.
  • Output length — The total number of characters in the Base64-encoded result, always a multiple of 4 (padding with '=' characters as needed).
  • 6-bit chunk — Each group of 6 bits maps to a single Base64 character from the 64-character alphabet (A–Z = 0–25, a–z = 26–51, 0–9 = 52–61, + = 62, / = 63).
  • = (padding) — One or two '=' characters appended when n mod 3 ≠ 0, to ensure the output length is always divisible by 4.

Worked Example: Encoding 'Man' to Base64

Input text: Man (3 ASCII bytes: M = 77 = 01001101, a = 97 = 01100001, n = 110 = 01101110)
Step 1 — Concatenate the 24 bits: 010011010110000101101110
Step 2 — Split into four 6-bit groups: 010011 | 010110 | 000101 | 101110
Step 3 — Convert each 6-bit group to decimal: 19, 22, 5, 46
Step 4 — Map to Base64 alphabet: 19→T, 22→W, 5→F, 46→u
Step 5 — No padding needed (n=3, divisible by 3)
Output length check: ⌈3/3⌉ × 4 = 1 × 4 = 4 characters

Result: TWFu

What Your Result Means

The plain text Man (3 bytes) encodes to the 4-character Base64 string TWFu — no padding required because the input length is exactly divisible by 3. Decoding TWFu through the reverse mapping returns Man exactly. If the input were only 1 byte (e.g., M), the result would be TQ== (two = padding characters); for 2 bytes (e.g., Ma), the result would be TWE= (one = padding character).

Understanding Base64

What Is Base64 and Why Is It Used?

Base64 is a binary-to-text encoding scheme defined in RFC 4648. It was designed to safely transmit binary data — such as images, files, or cryptographic keys — over channels that were originally designed to carry only ASCII text, such as SMTP email or HTTP headers.

The Base64 Alphabet

The 64 characters used are:

  • A–Z (indices 0–25)
  • a–z (indices 26–51)
  • 0–9 (indices 52–61)
  • + (index 62)
  • / (index 63)
  • = is used exclusively as a padding character.

URL-Safe Base64

Standard Base64 uses + and /, which have special meanings in URLs. URL-safe Base64 (also defined in RFC 4648) replaces + with - and / with _, making the encoded string safe for use directly in URLs and filenames without percent-encoding.

Encoding vs. Encryption

Base64 is not encryption. It provides no security — anyone can decode a Base64 string without a key. It exists purely for data compatibility and transport, not confidentiality. Do not use Base64 to protect sensitive data.

Size Overhead

Base64 encoding increases the data size by approximately 33% (every 3 bytes become 4 characters). For example, a 900 KB image becomes approximately 1,200 KB when Base64-encoded. This overhead is the trade-off for text-safe compatibility.

Common Use Cases

  • Embedding images directly in HTML or CSS (data:image/png;base64,...)
  • Encoding email attachments in MIME format
  • Passing binary data in JSON APIs
  • Storing cryptographic keys and certificates (PEM format)
  • Basic HTTP authentication (Authorization: Basic <base64(user:pass)>)

Common Mistakes

  • **Treating Base64 as encryption:** Base64 is encoding, not encryption. Any decoded result is immediately readable by anyone — never use it to hide sensitive information.
  • **Missing padding characters:** A valid Base64 string's length must be divisible by 4. Missing `=` padding at the end will cause a decoding error. If you trimmed trailing `=` signs, add them back before decoding.
  • **Using wrong variant (standard vs. URL-safe):** Standard Base64 uses `+` and `/`, while URL-safe Base64 uses `-` and `_`. Mixing variants causes incorrect decoding. Make sure you know which variant your source used.
  • **Encoding an already-encoded string:** Double-encoding (encoding a Base64 string again) produces a longer, nested Base64 string. Always start from the raw original input.
  • **Assuming Base64 handles Unicode correctly:** Base64 encodes bytes, not characters. Non-ASCII Unicode characters (e.g., emojis or accented letters) must first be UTF-8 encoded before Base64 encoding to ensure correct results.

Common Questions About Base64

How do I decode a Base64 string in JavaScript?

Use the built-in atob() function: atob('SGVsbG8=') returns 'Hello'. For encoding, use btoa('Hello') which returns 'SGVsbG8='. For Node.js, use Buffer.from('SGVsbG8=', 'base64').toString('utf8').

What happens if I try to decode an invalid Base64 string?

Decoding fails with an error. Valid Base64 strings contain only A–Z, a–z, 0–9, +, / (or - and _ for URL-safe), with optional = padding at the end, and a total length divisible by 4.

How is Base64 different from hexadecimal encoding?

Hexadecimal uses 16 characters (0–9, A–F) and represents each byte as 2 hex digits, doubling the size. Base64 uses 64 characters and represents 3 bytes as 4 characters, giving ~33% overhead — making Base64 more space-efficient than hex.

Is Base64 encoding reversible?

Yes, Base64 is fully reversible (lossless). Decoding a Base64 string always returns the exact original bytes, as long as the encoded string is valid and complete.

Frequently Asked Questions

Is Base64 the same as encryption?

No. Base64 is an encoding scheme, not encryption. It has no key and provides zero security — any Base64 string can be decoded by anyone instantly. Use proper encryption (such as AES) if you need to protect data.

Why does a Base64 string sometimes end with '=' or '=='?

The '=' characters are padding. Base64 output must always be a multiple of 4 characters long. When the input byte count isn't divisible by 3, one or two '=' characters are appended to reach the required length. One '=' means 2 remaining input bytes; '==' means 1 remaining input byte.

What is URL-safe Base64 and when should I use it?

URL-safe Base64 replaces '+' with '-' and '/' with '_' to avoid characters that have special meanings in URLs. Use URL-safe Base64 when embedding encoded data in a URL query parameter, path segment, or filename.

Does Base64 encoding increase file size?

Yes, by approximately 33%. Every 3 bytes of input become 4 characters of output. A 1 MB binary file becomes roughly 1.33 MB when Base64-encoded.

Can I Base64-encode an image?

Yes. Browsers support inline images via data URIs: set the src attribute to 'data:image/png;base64,' followed by the Base64-encoded image bytes. This removes the need for a separate HTTP request but increases page size.

Related Calculators

Sources

Only sources that have been reviewed are shown. Unverified citations are never published.

Spotted a calculation error?Report an Error
How this calculator works:MethodologyReport an Error

Report an issue or suggest a feature

Spotted an inaccuracy or have an idea to improve this calculator? Let us know.

We use cookies to operate the site, and — with your consent — to show advertising and measure usage. You can change your choice anytime in Cookie Settings.

Manage options