Base64 Encoding & Decoding Code Examples
Copy-paste Base64 encoding and decoding examples for JavaScript, Python, PHP, Go, Java, Rust, C#, Ruby, and more.
Base64 Encode a String in JavaScript
javascriptUse the built-in btoa() function to encode a UTF-8 string to Base64 in the browser or Node.js.
// Browser / Node.js 16+
const encoded = btoa('Hello, World!');
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// For Unicode strings, encode to UTF-8 first
function toBase64(str) {
return btoa(unescape(encodeURIComponent(str)));
}
console.log(toBase64('Привет')); // "0J/RgNC40LLQtdGC"
Base64 Decode a String in JavaScript
javascriptUse atob() to decode Base64 back to a plain string.
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
console.log(decoded); // "Hello, World!"
// For Unicode strings
function fromBase64(b64) {
return decodeURIComponent(escape(atob(b64)));
}
console.log(fromBase64('0J/RgNC40LLQtdGC')); // "Привет"
Base64 Encode in Python
pythonUse the base64 module from the standard library.
import base64
# Encode a string
encoded = base64.b64encode(b'Hello, World!').decode('utf-8')
print(encoded) # "SGVsbG8sIFdvcmxkIQ=="
# Encode from a variable
text = "Hello, World!"
encoded = base64.b64encode(text.encode('utf-8')).decode('utf-8')
print(encoded) # "SGVsbG8sIFdvcmxkIQ=="
Base64 Decode in Python
pythonDecode Base64-encoded data back to bytes or string.
import base64
encoded = 'SGVsbG8sIFdvcmxkIQ=='
decoded = base64.b64decode(encoded).decode('utf-8')
print(decoded) # "Hello, World!"
# URL-safe Base64
url_safe = base64.urlsafe_b64encode(b'data with +/=').decode()
print(url_safe) # Uses - and _ instead of + and /
Base64 Encode in PHP
phpPHP has built-in base64_encode() and base64_decode() functions.
<?php
$encoded = base64_encode('Hello, World!');
echo $encoded; // "SGVsbG8sIFdvcmxkIQ=="
// Encode binary data (e.g., file contents)
$fileData = file_get_contents('image.png');
$base64Image = base64_encode($fileData);
echo 'data:image/png;base64,' . $base64Image;
Base64 Decode in PHP
phpDecode Base64 strings with validation.
<?php
$decoded = base64_decode('SGVsbG8sIFdvcmxkIQ==');
echo $decoded; // "Hello, World!"
// With strict mode (returns false on invalid input)
$result = base64_decode('invalid!@#', true);
if ($result === false) {
echo "Invalid Base64 input";
}
Base64 Encode in Go
goUse the encoding/base64 package from the standard library.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := "Hello, World!"
// Standard encoding
encoded := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(encoded) // "SGVsbG8sIFdvcmxkIQ=="
// URL-safe encoding
urlEncoded := base64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(urlEncoded)
}
Base64 Decode in Go
goDecode Base64 with error handling.
package main
import (
"encoding/base64"
"fmt"
"log"
)
func main() {
encoded := "SGVsbG8sIFdvcmxkIQ=="
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
log.Fatal("Decode error:", err)
}
fmt.Println(string(decoded)) // "Hello, World!"
}
Base64 Encode in Java
javaUse java.util.Base64 (Java 8+).
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class Base64Example {
public static void main(String[] args) {
String text = "Hello, World!";
// Encode
String encoded = Base64.getEncoder()
.encodeToString(text.getBytes(StandardCharsets.UTF_8));
System.out.println(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// URL-safe encoding
String urlSafe = Base64.getUrlEncoder()
.encodeToString(text.getBytes(StandardCharsets.UTF_8));
}
}
Base64 Decode in Java
javaDecode Base64 back to string in Java.
import java.util.Base64;
import java.nio.charset.StandardCharsets;
String encoded = "SGVsbG8sIFdvcmxkIQ==";
byte[] decodedBytes = Base64.getDecoder().decode(encoded);
String decoded = new String(decodedBytes, StandardCharsets.UTF_8);
System.out.println(decoded); // "Hello, World!"
Base64 Encode in Rust
rustUse the base64 crate (add base64 = "0.22" to Cargo.toml).
// Cargo.toml: base64 = "0.22"
use base64::{Engine as _, engine::general_purpose};
fn main() {
let data = "Hello, World!";
let encoded = general_purpose::STANDARD.encode(data);
println!("{}", encoded); // "SGVsbG8sIFdvcmxkIQ=="
// URL-safe encoding
let url_safe = general_purpose::URL_SAFE.encode(data);
println!("{}", url_safe);
}
Base64 Decode in Rust
rustDecode Base64 with proper error handling in Rust.
use base64::{Engine as _, engine::general_purpose};
fn main() {
let encoded = "SGVsbG8sIFdvcmxkIQ==";
match general_purpose::STANDARD.decode(encoded) {
Ok(bytes) => {
let text = String::from_utf8(bytes).unwrap();
println!("{}", text); // "Hello, World!"
}
Err(e) => eprintln!("Decode error: {}", e),
}
}
Base64 Encode in C#
csharpUse System.Convert for Base64 in .NET.
using System;
using System.Text;
string text = "Hello, World!";
byte[] bytes = Encoding.UTF8.GetBytes(text);
string encoded = Convert.ToBase64String(bytes);
Console.WriteLine(encoded); // "SGVsbG8sIFdvcmxkIQ=="
Base64 Decode in C#
csharpDecode Base64 string back to text in C#.
using System;
using System.Text;
string encoded = "SGVsbG8sIFdvcmxkIQ==";
byte[] bytes = Convert.FromBase64String(encoded);
string decoded = Encoding.UTF8.GetString(bytes);
Console.WriteLine(decoded); // "Hello, World!"
Base64 Encode & Decode in Ruby
rubyRuby's Base64 module provides strict and URL-safe encoding.
require 'base64'
# Encode
encoded = Base64.strict_encode64('Hello, World!')
puts encoded # "SGVsbG8sIFdvcmxkIQ=="
# Decode
decoded = Base64.strict_decode64('SGVsbG8sIFdvcmxkIQ==')
puts decoded # "Hello, World!"
# URL-safe variant
url_safe = Base64.urlsafe_encode64('data with +/=')
puts url_safe # Uses - and _ instead of + and /
Base64 Encode & Decode in Bash
bashUse the base64 command-line utility available on Linux and macOS.
# Encode a string
echo -n "Hello, World!" | base64
# Output: SGVsbG8sIFdvcmxkIQ==
# Decode a string
echo "SGVsbG8sIFdvcmxkIQ==" | base64 --decode
# Output: Hello, World!
# Encode a file
base64 < input.txt > encoded.txt
# Decode a file
base64 --decode < encoded.txt > output.txt
Need to work with base64 encode/decode right now?
Use our free online base64 encoder — no signup, no install, works in your browser.
Open Base64 Encoder →