Encoding 2026-03-10

Fix Base64 Encoding & Decoding Errors

Solve Base64 padding errors, Unicode decode failures, URL-unsafe character issues, and binary data corruption when using the Base64 Encoder tool.

🔐 Tool: Base64 Encoder — Free

Base64 looks simple — it's just letters and numbers — but padding characters, Unicode edge cases, and URL-unsafe variants trip up developers constantly. Here are the six most common errors and their fixes.

Jump to error

  1. 1 Invalid Base64 padding error
  2. 2 Unicode characters produce garbled output
  3. 3 Base64 string breaks URLs (+, /, = characters)
  4. 4 Spaces or newlines in Base64 string cause decode failure
  5. 5 Decoded binary file is corrupted
1

Invalid Base64 padding error

Error message
InvalidCharacterError: Failed to execute 'atob': The string to be decoded is not correctly encoded.
Root cause

Base64 output length must be a multiple of 4. Missing `=` padding characters cause the decoder to fail.

Step-by-step fix

  1. 1 Check the length of your Base64 string (mod 4).
  2. 2 If `length % 4 === 2`, add `==` at the end.
  3. 3 If `length % 4 === 3`, add `=` at the end.
  4. 4 Paste the padded string into the decoder.
Wrong
SGVsbG8gV29ybGQ
Correct
SGVsbG8gV29ybGQ=

2

Unicode characters produce garbled output

Error message
DOMException: Failed to execute 'btoa': The string to be encoded contains characters outside of the Latin1 range.
Root cause

The browser's native `btoa()` only handles Latin-1 (0–255). Multi-byte Unicode characters (emojis, CJK) require UTF-8 encoding first.

Step-by-step fix

  1. 1 Use the tool's built-in Unicode support (it handles UTF-8 automatically).
  2. 2 In your own code, encode via TextEncoder before Base64: `btoa(String.fromCharCode(...new TextEncoder().encode(str)))`.
  3. 3 Or use a library like `js-base64` which handles Unicode natively.
Wrong
btoa('Hello 🌍')  // throws
Correct
// Use the tool's Unicode mode, or:
const bytes = new TextEncoder().encode('Hello 🌍');
const b64 = btoa(String.fromCharCode(...bytes));

3

Base64 string breaks URLs (+, /, = characters)

Error message
400 Bad Request — invalid characters in query parameter
Root cause

Standard Base64 uses `+`, `/`, and `=` which are reserved in URLs. Use Base64url variant instead.

Step-by-step fix

  1. 1 In the encoder tool, select 'URL-safe' mode if available.
  2. 2 Or manually replace: `+` → `-`, `/` → `_`, strip trailing `=`.
  3. 3 When decoding, reverse the substitutions before decoding.
Wrong
https://api.example.com/token?t=SGVs+bG8=
Correct
https://api.example.com/token?t=SGVs-bG8

4

Spaces or newlines in Base64 string cause decode failure

Error message
Error: Invalid character encountered
Root cause

Base64 strings copied from emails, PDFs, or multi-line certificates often contain line breaks or spaces that break decoding.

Step-by-step fix

  1. 1 Remove all whitespace from the Base64 string before decoding.
  2. 2 In JS: `base64.replace(/\s/g, '')`.
  3. 3 The tool's decoder automatically strips whitespace — paste raw and decode.
Wrong
SGVsbG8g V29y
bGQ=
Correct
SGVsbG8gV29ybGQ=

5

Decoded binary file is corrupted

Error message
(No error — output file is unreadable)
Root cause

Base64 decode returns binary bytes, not a UTF-8 string. Treating binary data as text introduces encoding corruption.

Step-by-step fix

  1. 1 Use the tool's 'Download as file' option for binary output.
  2. 2 In code, decode to a Uint8Array, not a string: `Uint8Array.from(atob(b64), c => c.charCodeAt(0))`.
  3. 3 Create a Blob and use URL.createObjectURL() to serve the file.
Wrong
const text = atob(base64ImageData); // corrupts binary
Correct
const bytes = Uint8Array.from(atob(base64ImageData), c => c.charCodeAt(0));
const blob = new Blob([bytes], {type: 'image/png'});

Frequently Asked Questions

Is Base64 encoding encryption?

No. Base64 is encoding, not encryption. Anyone can decode it instantly. Never use Base64 to protect sensitive data — use proper encryption (AES, RSA) instead.

Why does my Base64 output differ between tools?

Different tools use different line-wrap settings (RFC 2045 wraps at 76 chars, RFC 4648 does not) and may use standard vs URL-safe alphabets. Strip whitespace and check the alphabet used.

How much does Base64 increase file size?

Base64 encoding increases data size by approximately 33% (4 output bytes for every 3 input bytes). Factor this in when storing Base64-encoded blobs in databases.

Related Tools

Try the Base64 Encoder now

Free, runs in your browser, no signup required. Learn more about Base64 Encoder.

Open Base64 Encoder →