Base64 Encoder & Decoder
Encode clear text strings into Base64 format or decode Base64 strings back to text. Supports full UTF-8 encoding (accents, emojis) 100% offline.
RFC 4648 Base64 Encoding Architecture & 6-Bit Chunking
Base64 is a binary-to-text encoding scheme defined formally by RFC 4648. It is designed to represent arbitrary sequences of binary bytes using a safe, restricted alphabet of exactly 64 printable ASCII characters (A-Z, a-z, 0-9, +, and /, with = reserved for padding).
The mathematical operation of Base64 is based on 6-bit chunking. Because an octet (byte) contains 8 bits, every group of three 8-bit bytes (24 bits total) can be divided evenly into four 6-bit chunks (4 * 6 = 24). Each 6-bit value (ranging from 0 to 63) maps directly to a character in the standard Base64 lookup index table:
- Bits
0β25map to uppercase lettersAβZ. - Bits
26β51map to lowercase lettersaβz. - Bits
52β61map to numerals0β9. - Bits
62and63map to+and/respectively.
If the input byte length is not divisible by 3, trailing padding characters (= or ==) are appended so the output length is always a multiple of 4. Because 4 ASCII characters represent 3 raw bytes, Base64 introduces a predictable 33% data size overhead.
Programmatic Base64 Encoding in JavaScript, Python & CLI
Execute UTF-8 safe Base64 encoding and decoding using these verified snippets:
// Modern JavaScript (UTF-8 Safe with TextEncoder)
function base64Encode(str) {
const bytes = new TextEncoder().encode(str);
const binString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join('');
return btoa(binString);
}
function base64Decode(b64) {
const binString = atob(b64);
const bytes = Uint8Array.from(binString, (m) => m.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
const encoded = base64Encode("Hello, δΈη! π");
console.log("Encoded:", encoded);
console.log("Decoded:", base64Decode(encoded));
# Python 3 base64 module
import base64
text = "Hello, δΈη! π"
# Encode
raw_bytes = text.encode("utf-8")
b64_str = base64.b64encode(raw_bytes).decode("ascii")
print("Base64:", b64_str)
# Decode
decoded_str = base64.b64decode(b64_str.encode("ascii")).decode("utf-8")
print("Original:", decoded_str)
# Bash / Terminal CLI
echo -n "Hello World" | base64
# Decode
echo -n "SGVsbG8gV29ybGQ=" | base64 --decode
UTF-8 Safety & The Native btoa() Browser Bug
A notorious trap in client-side JavaScript is that the legacy browser functions window.btoa() and window.atob() only accept characters within the Latin-1 (ASCII 0β255) code range. Attempting to encode Unicode characters like accents (Γ‘, Γ±, ΓΌ) or emojis throws a fatal DOMException: The string to be encoded contains characters outside of the Latin1 range.
Our tool implements modern TextEncoder and TextDecoder byte streaming, ensuring 100% error-free translation for all international languages and emoji characters.
Standard Base64 vs Base64URL (RFC 4648 Section 5)
In web URLs, query parameters, and JWT tokens, standard Base64 characters + and / cause routing failures, while = padding can be stripped or misinterpreted. Base64URL replaces + with hyphen (-), replaces / with underscore (_), and omits trailing = padding characters to ensure safe URL transmission.
RFC 4648 Bit-Shifting Mechanics: 6-Bit Sextets vs 8-Bit Octets
Base64 encoding solves a historical transmission vulnerability: legacy email gateways and binary-unclean protocols frequently strip or corrupt the highest-order bit of 8-bit binary octets. The encoding process, formalized in RFC 4648, maps continuous streams of 8-bit bytes into a 64-character alphanumeric alphabet (A-Z, a-z, 0-9, +, /).
Every group of three 8-bit bytes (24 bits total) is segmented into four 6-bit chunks (sextets), where each 6-bit value (0 to 63) indexes an ASCII glyph. If the total input byte count is not divisible by 3, padding characters (=) are appended to preserve 4-character block alignment. In URL transmission contexts, standard + and / characters are replaced with - and _ respectively to prevent clash with URI reserved delimiters.
Encoding Efficiency & Boundary Calculations
- 33.3% Overhead Factor: Expanding 3 bytes into 4 characters predictably increases payload size by exactly 4/3 (~33.33%).
- Padding Rules: 1 remaining byte requires two Base64 glyphs followed by
==; 2 remaining bytes require three glyphs followed by=. - Surrogate Pair Safety: Our browser engine decodes UTF-8 multibyte characters via TextEncoder before bit-shifting, preventing truncation of emoji and international characters.
Frequently Asked Questions
No. Base64 is an encoding format, not encryption. It provides zero confidentiality or security. Anyone who receives a Base64 string can instantly decode it back to plaintext.
Because Base64 represents 3 bytes (24 bits) using 4 ASCII characters (32 bits). The ratio 32/24 represents an exact 33.33% size expansion.
Padding signals to the decoder how many zero-bits were added to align the final byte chunk to a 24-bit boundary.
Yes. Base64 can encode any sequence of raw bytes, including PNG, JPEG, PDF, and ZIP files, into Data URLs for embedding directly into HTML or CSS.
No. The conversion is performed 100% locally in your browser using client-side JavaScript.