Base64 encode and decode in every language

Copy-paste examples for the languages people ask about most. Each one encodes UTF-8 text, decodes it again, and shows the URL-safe variant. To check a result, paste it into the decoder.

Java

import java.nio.charset.StandardCharsets;
import java.util.Base64;

String b64 = Base64.getEncoder().encodeToString("Hello, World!".getBytes(StandardCharsets.UTF_8));
String text = new String(Base64.getDecoder().decode(b64), StandardCharsets.UTF_8);

// URL-safe, no padding (JWT style)
String url = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
// MIME (76-character lines) - tolerant of line breaks when decoding
String mime = Base64.getMimeEncoder().encodeToString(bytes);
byte[] back = Base64.getMimeDecoder().decode(mime);

JavaScript (browser)

btoa and atob only handle Latin-1, so go through bytes for real text:

const bytes = new TextEncoder().encode("Hello, World! สวัสดี");
const b64 = btoa(String.fromCharCode(...bytes));          // fine for small data

const back = Uint8Array.from(atob(b64), c => c.charCodeAt(0));
const text = new TextDecoder().decode(back);

// newer browsers: Uint8Array.prototype.toBase64 / Uint8Array.fromBase64
// const b64 = bytes.toBase64({ alphabet: "base64url", omitPadding: true });

Node.js

const b64 = Buffer.from("Hello, World!", "utf8").toString("base64");
const text = Buffer.from(b64, "base64").toString("utf8");
const url = Buffer.from(data).toString("base64url");       // Node 16+
const file = require("fs").readFileSync("report.pdf").toString("base64");

Python

import base64

b64 = base64.b64encode("Hello, World!".encode("utf-8")).decode("ascii")
text = base64.b64decode(b64).decode("utf-8")

url = base64.urlsafe_b64encode(data).rstrip(b"=")          # JWT style
padded = s + "=" * (-len(s) % 4)                           # add missing padding before decoding
data = base64.urlsafe_b64decode(padded)

C# / .NET

using System.Text;

string b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello, World!"));
string text = Encoding.UTF8.GetString(Convert.FromBase64String(b64));

// URL-safe (.NET 9+): System.Buffers.Text.Base64Url.EncodeToString(bytes)
string url = b64.TrimEnd('=').Replace('+', '-').Replace('/', '_');

Go

import "encoding/base64"

b64 := base64.StdEncoding.EncodeToString([]byte("Hello, World!"))
data, err := base64.StdEncoding.DecodeString(b64)

url := base64.RawURLEncoding.EncodeToString(data)        // no padding

PHP

$b64  = base64_encode("Hello, World!");
$text = base64_decode($b64, true);                          // strict: false on invalid input

$url  = rtrim(strtr(base64_encode($data), '+/', '-_'), '=');

Bash / command line

echo -n 'Hello, World!' | base64          # -n: no trailing newline!
echo 'SGVsbG8sIFdvcmxkIQ==' | base64 -d
base64 -w 0 report.pdf > report.b64       # GNU: one line (macOS: base64 -i report.pdf)

# Windows PowerShell
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello, World!"))
certutil -encode report.pdf report.b64

SQL

-- PostgreSQL
SELECT encode(convert_to('Hello, World!', 'UTF8'), 'base64');
SELECT convert_from(decode('SGVsbG8sIFdvcmxkIQ==', 'base64'), 'UTF8');

-- MySQL 5.6+
SELECT TO_BASE64('Hello, World!'), FROM_BASE64('SGVsbG8sIFdvcmxkIQ==');

Common mistakes

  • A trailing newline from echo changes the result - use echo -n.
  • Wrong character set: encoding Thai or emoji with Latin-1 loses them. Use UTF-8 unless the other side says otherwise.
  • URL-safe vs standard: a strict standard decoder rejects - and _. Convert with the Base64URL tool.
  • Missing padding: Python and some others need = signs; add them as shown above.
  • Data URI prefix: strip data:...;base64, before decoding.
Ad