Python 3: base64.b64decode(value).decode("utf-8") decodes; base64.b64encode(value.encode("utf-8")).decode("ascii") encodes. JavaScript: Buffer.from(value, "base64").toString("utf8") in Node, or atob(value) with TextDecoder in the browser. Command line: printf '%s' "$value" | base64 -d on GNU, base64 -D on macOS. All three produce the same decoded value. When they fail, the cause is usually padding, URL-safe characters or the wrong text encoding.
The Same String in Five Environments
Use one payload so the differences are visible. This is the Base64 for Hello, World!:
SGVsbG8sIFdvcmxkIQ==
| Environment | Code | Output |
| Python 3.12 | base64.b64decode(s).decode("utf-8") | Hello, World! |
| Node 24 | Buffer.from(s, "base64").toString("utf8") | Hello, World! |
| Browser | new TextDecoder().decode(Uint8Array.from(atob(s), c => c.charCodeAt(0))) | Hello, World! |
| PowerShell 5.1 | [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($s)) | Hello, World! |
| GNU base64 | base64 -d | Hello, World |
The Python, Node and PowerShell outputs were run on 25 November 2026. The browser row uses the same atob and TextDecoder APIs and was checked in Node. The GNU and macOS commands follow their published manuals.

How Base64 Turns Bytes into Characters
Base64 reads the input in groups of three bytes and writes four characters for each group. The alphabet is A-Z, a-z, 0-9, + and /; = is padding, not data. If the last group has one byte, it becomes two characters plus ==. If it has two bytes, it becomes three characters plus =. That is why a 13-byte string becomes 20 characters, and why a missing = appears as a padding error rather than a wrong character.
The decoder reverses the mapping, but it cannot know what the bytes mean. Text, a PNG and a gzip stream all look like Base64 on the way in. The bytes-to-text step is where most "it decoded but it is garbage" cases fail.
Python 3: Decode Base64 bytes, then Decode text
The examples use Python 3.12. The same b64decode call works in python3 on macOS and Linux, and in Python 3.8 and later. base64.b64decode() returns bytes. It does not know whether those bytes are UTF-8, UTF-16 or a PNG. The second step is yours:
import base64
value = "SGVsbG8sIFdvcmxkIQ=="
raw = base64.b64decode(value)
text = raw.decode("utf-8")
print(text) # Hello, World!
Two arguments matter when the input is messy.
validate=False is the default. It discards characters outside the Base64 alphabet before it checks padding. A string with line breaks from an email or a JSON file will decode. validate=True rejects those characters instead:
base64.b64decode("SGVsbG8s\nIFdvcmxkIQ==") # b'Hello, World!'
base64.b64decode("SGVsbG8s\nIFdvcmxkIQ==", validate=True) # binascii.Error
Missing padding raises binascii.Error: Incorrect padding. Add it back before decoding:
def decode_base64(value: str) -> bytes:
value = "".join(value.split())
value += "=" * (-len(value) % 4)
return base64.b64decode(value, validate=True)
If the length leaves a remainder of 1, the string is truncated or corrupt. Padding will not repair it. URL-safe Base64 replaces + with - and / with _. Python has a decoder for that, but it still expects padding:
base64.urlsafe_b64decode("SGVsbG8sIFdvcmxkIQ==") # b'Hello, World!'
base64.urlsafe_b64decode("SGVsbG8sIFdvcmxkIQ") # binascii.Error: Incorrect padding
For files, read the bytes and decode, or use base64.decode(input_file, output_file) with binary file objects to stream from one file to another. The second form writes raw bytes, not text. If you need the reverse direction, the Base64 Encode tool turns text or a file into a string.
Base64 makes data about a third larger: every three bytes become four characters. The Data Size Converter will size the result when a payload has to fit a fixed limit. Short strings can be worse than a third because of padding. Hello, World! is 13 bytes and 20 Base64 characters.

Python Base64 Encode: bytes to text
base64.b64encode() takes bytes and returns bytes. To encode in Python 3, encode the text first, then decode the Base64 result to ASCII for JSON, headers or logs:
import base64
raw = "Hello, World!".encode("utf-8")
encoded = base64.b64encode(raw).decode("ascii")
print(encoded) # SGVsbG8sIFdvcmxkIQ==
For URL-safe output, use base64.urlsafe_b64encode(raw).decode("ascii"). A payload such as b"\xfb\xff" becomes -_8= instead of +/8=. The standard library's base64 module is the python encoder for this job; it is not a general text encoder, a compressor or encryption. To reverse the step, call b64decode on the string.
A common python base64 encode mistake is passing a str directly. b64encode expects bytes, so base64.b64encode("Hello") raises TypeError; call .encode("utf-8") first. The same rule applies in Python 3 and python3: text and bytes are different types.
Decode a file, not just a string
The same calls work on file bytes. In Python:
from pathlib import Path
import base64
raw = base64.b64decode(Path("payload.b64").read_text().strip())
Path("payload.bin").write_bytes(raw)
In Node:
const fs = require("node:fs");
const raw = Buffer.from(fs.readFileSync("payload.b64", "utf8"), "base64");
fs.writeFileSync("payload.bin", raw);
On the command line, redirect instead of printing:
base64 -d payload.b64 > payload.bin
Then check the first bytes, not the file extension. PNG starts 89 50 4E 47, PDF starts 25 50 44 46, gzip starts 1F 8B, and ZIP starts 50 4B 03 04. On macOS or Linux, file payload.bin names the format. A Base64 string that decodes to a file is not an error; treating those bytes as UTF-8 is.
JavaScript: atob is not a UTF-8 decoder
In a browser, atob() decodes Base64 to a binary string. Each character is a byte, not a Unicode code point. If the bytes are UTF-8, atob() alone gives mojibake:
const binary = atob("aMOpbGxv"); // "héllo"
const text = new TextDecoder().decode(
Uint8Array.from(binary, (char) => char.charCodeAt(0))
);
console.log(text); // "héllo"
btoa() has the opposite limit. It accepts only characters in the Latin-1 range. btoa("héllo") returns aOlsbG8=, which is the Latin-1 byte for é, not its UTF-8 bytes. btoa("你好") throws InvalidCharacterError. To encode Unicode, convert to UTF-8 bytes first:
const bytes = new TextEncoder().encode("héllo");
const base64 = btoa(String.fromCharCode(...bytes));
In Node, Buffer does both steps:
const text = Buffer.from("SGVsbG8sIFdvcmxkIQ==", "base64").toString("utf8");
console.log(text); // Hello, World!
Node's decoder is lenient. It accepts URL-safe characters and missing padding, which helps with JWT segments but can hide a corrupt input. Browser atob() rejects - and _. Convert them before decoding:
const standard = value.replace(/-/g, "+").replace(/_/g, "/");
const padded = standard + "=".repeat((4 - (standard.length % 4)) % 4);
const binary = atob(padded);
Do not call atob() and assume the result is text. If the payload is a PNG, a PDF or a gzip stream, write the bytes to a file and inspect the signature.
Command line: GNU, macOS and PowerShell
GNU base64 decodes with -d or --decode:
printf '%s' "$VALUE" | base64 -d
printf avoids the trailing newline that echo adds. Newlines inside the input are always accepted. -i or --ignore-garbage skips other unrecognized bytes, which is useful for copied text with stray characters. By default, encoding wraps at 76 characters; -w 0 disables wrapping when the output goes into JSON or an environment variable.
base64 -d input.b64 > output.bin
base64 -w 0 input.bin > output.b64
macOS ships a different base64. It decodes with -D or -d, and it does not wrap by default:
base64 -D -i input.b64 -o output.bin
PowerShell uses .NET rather than a command named base64:
$bytes = [Convert]::FromBase64String($s)
[Text.Encoding]::UTF8.GetString($bytes)
FromBase64String ignores tabs, spaces, CR and LF. It throws FormatException when the length, ignoring whitespace, is not a multiple of 4, or when the string contains a non-Base64 character. In Windows PowerShell the wrapper reports that as MethodInvocationException, so read the inner message.
JWT and other URL-safe payloads
A JWT's middle segment is Base64URL: - and _ replace + and /, and the padding is usually removed. In Python, urlsafe_b64decode handles the characters but still needs padding:
import base64
segment = "eyJzdWIiOiIxMjM0NTY3ODkwIn0"
padded = segment + "=" * (-len(segment) % 4)
text = base64.urlsafe_b64decode(padded).decode("utf-8")
print(text)
In Node, Buffer.from(segment, "base64").toString("utf8") accepts the URL-safe form directly. In the browser, replace - with + and _ with /, add padding, then call atob. Do not decode the signature segment as text; it is bytes, and the header and payload are the parts you can read.The same rule applies to any URL-safe value: normalize the alphabet and the padding before decoding.
The Failures behind most Base64 questions
Before you change code, check the length after whitespace, the alphabet (- and _ mean Base64URL), any data: or PEM wrapper, and the text encoding. Those four checks explain most failed decodes.
| Symptom | Cause | Fix |
| binascii.Error: Incorrect padding | Missing = or a truncated string | Add padding with -len(value) % 4; if the remainder is 1, the input is corrupt |
| InvalidCharacterError from atob | URL-safe - or _, or a character outside the alphabet | Replace - with + and _ with /, then pad |
| UnicodeDecodeError | The bytes are not UTF-8 | Try UTF-16LE, Windows-1252 or the encoding the source used |
| A gap between every letter | UTF-16LE decoded as UTF-8 | Decode as UTF-16LE; the zero byte after each ASCII character causes the gap |
| Only base64 data is allowed | validate=True and the input has whitespace or newlines | Strip the whitespace or use validate=False |
| Garbled text after a data: prefix | The prefix was decoded as Base64 characters | Remove everything through ;base64, first |
| b'...' instead of text | Python returned bytes and the code skipped .decode() | Add .decode("utf-8") or the correct encoding |
| A .bin file that will not open | The bytes are a file, not text | Write the decoded bytes to a file and check the signature |
| An extra blank line in the output | echo added a newline before encoding | Use printf '%s' instead of echo |

A data:image/png;base64, string needs the prefix removed. Python's permissive decoder will treat the letters in data as Base64 digits and produce the wrong bytes. The same applies to PEM armor: remove the -----BEGIN----- and -----END----- lines before decoding, or use a decoder that handles them.
For a payload you only need to inspect once, the Base64 Decode tool accepts URL-safe, unpadded, data URL and PEM input, reads the bytes as text, hex or binary, and offers 12 text encodings. It runs in the browser, so the string is not uploaded. Its previews stop at 64 KiB, but the download contains the full bytes; the input limit is ten million characters.






