base64_decode
base64_decode(string $string, bool $strict = false): string|false
Decodes a base64-encoded string back to the original bytes.
C C23
#include <stdio.h>
static int b64val(int c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1; // '=' and anything else
}
int main(void) {
const char *s = "SGVsbG8sIFdvcmxkIQ==";
int acc = 0, bits = 0;
for (const char *p = s; *p; p++) {
int v = b64val((unsigned char)*p);
if (v < 0) continue; // padding '=' contributes no bits
acc = (acc << 6) | v;
bits += 6;
if (bits >= 8) {
bits -= 8;
putchar((acc >> bits) & 0xFF);
}
}
putchar('\n');
return 0;
}
Hello, World!
C++ C++26
#include <iostream>
#include <string>
static int b64val(int c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1; // '=' and anything else
}
int main() {
std::string s = "SGVsbG8sIFdvcmxkIQ==";
unsigned acc = 0;
int bits = 0;
for (char ch : s) {
int v = b64val((unsigned char)ch);
if (v < 0) continue;
acc = (acc << 6) | (unsigned)v;
bits += 6;
if (bits >= 8) {
bits -= 8;
std::cout << (char)((acc >> bits) & 0xFF);
}
}
std::cout << "\n";
return 0;
}
Hello, World!
C# v11.0.100
using System;
byte[] raw = Convert.FromBase64String("SGVsbG8sIFdvcmxkIQ==");
Console.WriteLine(System.Text.Encoding.UTF8.GetString(raw));
Hello, World!
Go v1.27.4
package main
import (
"encoding/base64"
"fmt"
)
func main() {
s, err := base64.StdEncoding.DecodeString("SGVsbG8sIFdvcmxkIQ==")
if err != nil {
panic(err)
}
fmt.Println(string(s))
}
Hello, World!
Java v27.0.1
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Main {
public static void main(String[] args) {
byte[] raw = Base64.getDecoder().decode("SGVsbG8sIFdvcmxkIQ==");
System.out.println(new String(raw, StandardCharsets.UTF_8));
}
}
Hello, World!
JavaScript v26.13.1
console.log(Buffer.from("SGVsbG8sIFdvcmxkIQ==", "base64").toString("utf8"));
Hello, World!
PHP v8.6.0
echo base64_decode("SGVsbG8sIFdvcmxkIQ=="), "\n";
var_dump(base64_decode("###", true)); // strict mode rejects invalid characters
Hello, World! bool(false)
Python v3.15.2
import base64
print(base64.b64decode("SGVsbG8sIFdvcmxkIQ==").decode("utf-8"))
Hello, World!
Rust v1.99.1
fn b64val(c: u8) -> Option<u32> {
match c {
b'A'..=b'Z' => Some((c - b'A') as u32),
b'a'..=b'z' => Some((c - b'a') as u32 + 26),
b'0'..=b'9' => Some((c - b'0') as u32 + 52),
b'+' => Some(62),
b'/' => Some(63),
_ => None, // '=' padding lands here
}
}
fn main() {
let s = b"SGVsbG8sIFdvcmxkIQ==";
let mut acc = 0u32;
let mut bits = 0u32;
let mut raw = Vec::new();
for &c in s {
if let Some(v) = b64val(c) {
acc = (acc << 6) | v;
bits += 6;
if bits >= 8 {
bits -= 8;
raw.push(((acc >> bits) & 0xFF) as u8);
}
}
}
println!("{}", String::from_utf8(raw).unwrap());
}
Hello, World!
TypeScript v26.13.1
console.log(Buffer.from("SGVsbG8sIFdvcmxkIQ==", "base64").toString("utf8"));
Hello, World!
Language differences
C: no base64 in the standard library — hand-writes the six-bit accumulator like C++/Rust.
C++: no base64 in the standard library — hand-writes the six-bit accumulator.
C#: Convert.FromBase64String ignores whitespace and throws FormatException on other invalid input.
Go: requires picking StdEncoding (padded) vs RawStdEncoding (unpadded) correctly and returns an error on failure.
Java: Base64.getDecoder() strictly rejects invalid characters (lenient via getMimeDecoder()).
JavaScript: the browser's atob only handles single-byte latin1 while Node uses Buffer (UTF-8 multi-byte safe) — keep the two routes straight.
PHP: decoding pairs with base64_encode, and the algorithm produces identical output in all 10 languages; the differences are error policy and the bytes/text boundary — it defaults to lenient (skips invalid characters, possibly mixing in garbage), while strict = true returns false on invalid input (the demo's second line); semantic reminder: base64 decodes to bytes, not text, so the explicit .decode()/ToString step in every language block is not optional.
Python: b64decode also ignores invalid characters by default (validate=True raises binascii.Error) and returns bytes, so .decode("utf-8") is still needed for text.
Rust: std has no base64 (ecosystem: the base64 crate), so like C/C++ the demo hand-writes the six-bit accumulator.