Every mainstream language ships a Base64 implementation, but they differ in the details that cause bugs: Unicode handling, URL-safe alphabets, padding, line breaks and strictness. These guides show the idiomatic way to do it in each language, and the traps to avoid.
Quick reference
| Language | Encode | Decode |
|---|---|---|
| JavaScript (browser) | btoa(binary) / Uint8Array.prototype.toBase64() |
atob(b64) / Uint8Array.fromBase64() |
| Node.js | Buffer.from(data).toString('base64') |
Buffer.from(b64, 'base64') |
| Python | base64.b64encode(data) |
base64.b64decode(b64) |
| PHP | base64_encode($data) |
base64_decode($b64, true) |
| Java | Base64.getEncoder().encodeToString(bytes) |
Base64.getDecoder().decode(b64) |
| C# | Convert.ToBase64String(bytes) |
Convert.FromBase64String(b64) |
| Go | base64.StdEncoding.EncodeToString(b) |
base64.StdEncoding.DecodeString(s) |
| Bash | base64 -w0 file |
base64 -d file |
Three rules that prevent most Base64 bugs
- Base64 encodes bytes, not text. Convert strings to bytes with an explicit character set (almost always UTF-8) before encoding, and back after decoding.
- Know your alphabet. URLs, JWTs and file names use Base64URL (
-and_, usually no padding). Standard decoders reject it. - Be strict when validating, lenient when reading. Reject malformed input from APIs, but strip whitespace and data URI prefixes when users paste values.
Guides by topic
Choose a language or topic below.
Comments (0)
Questions, ideas or a bug in “Base64 for developers”? Leave a comment, rate the tool, or reply to others.
No comments yet. Be the first to share your feedback!