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.
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
Invalid Base64 padding error
InvalidCharacterError: Failed to execute 'atob': The string to be decoded is not correctly encoded.
Base64 output length must be a multiple of 4. Missing `=` padding characters cause the decoder to fail.
Step-by-step fix
- 1 Check the length of your Base64 string (mod 4).
- 2 If `length % 4 === 2`, add `==` at the end.
- 3 If `length % 4 === 3`, add `=` at the end.
- 4 Paste the padded string into the decoder.
SGVsbG8gV29ybGQ
SGVsbG8gV29ybGQ=
Unicode characters produce garbled output
DOMException: Failed to execute 'btoa': The string to be encoded contains characters outside of the Latin1 range.
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 Use the tool's built-in Unicode support (it handles UTF-8 automatically).
- 2 In your own code, encode via TextEncoder before Base64: `btoa(String.fromCharCode(...new TextEncoder().encode(str)))`.
- 3 Or use a library like `js-base64` which handles Unicode natively.
btoa('Hello 🌍') // throws
// Use the tool's Unicode mode, or:
const bytes = new TextEncoder().encode('Hello 🌍');
const b64 = btoa(String.fromCharCode(...bytes));
Base64 string breaks URLs (+, /, = characters)
400 Bad Request — invalid characters in query parameter
Standard Base64 uses `+`, `/`, and `=` which are reserved in URLs. Use Base64url variant instead.
Step-by-step fix
- 1 In the encoder tool, select 'URL-safe' mode if available.
- 2 Or manually replace: `+` → `-`, `/` → `_`, strip trailing `=`.
- 3 When decoding, reverse the substitutions before decoding.
https://api.example.com/token?t=SGVs+bG8=
https://api.example.com/token?t=SGVs-bG8
Spaces or newlines in Base64 string cause decode failure
Error: Invalid character encountered
Base64 strings copied from emails, PDFs, or multi-line certificates often contain line breaks or spaces that break decoding.
Step-by-step fix
- 1 Remove all whitespace from the Base64 string before decoding.
- 2 In JS: `base64.replace(/\s/g, '')`.
- 3 The tool's decoder automatically strips whitespace — paste raw and decode.
SGVsbG8g V29y bGQ=
SGVsbG8gV29ybGQ=
Decoded binary file is corrupted
(No error — output file is unreadable)
Base64 decode returns binary bytes, not a UTF-8 string. Treating binary data as text introduces encoding corruption.
Step-by-step fix
- 1 Use the tool's 'Download as file' option for binary output.
- 2 In code, decode to a Uint8Array, not a string: `Uint8Array.from(atob(b64), c => c.charCodeAt(0))`.
- 3 Create a Blob and use URL.createObjectURL() to serve the file.
const text = atob(base64ImageData); // corrupts binary
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 →