base64_encode
base64_encode(string $string): string
Encodes data with MIME base64 (output without line breaks).
C C23
#include <stdio.h>
static const char TABLE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int main(void) {
const char *s = "hello php";
int len = 9;
char out[32];
int j = 0;
for (int i = 0; i < len; i += 3) {
int n = (unsigned char)s[i] << 16
| (i + 1 < len ? (unsigned char)s[i + 1] << 8 : 0)
| (i + 2 < len ? (unsigned char)s[i + 2] : 0);
out[j++] = TABLE[(n >> 18) & 63];
out[j++] = TABLE[(n >> 12) & 63];
out[j++] = i + 1 < len ? TABLE[(n >> 6) & 63] : '=';
out[j++] = i + 2 < len ? TABLE[n & 63] : '=';
}
out[j] = '\0';
printf("%s\n", out);
return 0;
}
aGVsbG8gcGhw
C++ C++26
#include <iostream>
#include <string>
static const std::string TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int main() {
std::string s = "hello php";
std::string out;
for (size_t i = 0; i < s.size(); i += 3) {
unsigned n = (unsigned char)s[i] << 16;
if (i + 1 < s.size()) n |= (unsigned char)s[i + 1] << 8;
if (i + 2 < s.size()) n |= (unsigned char)s[i + 2];
out += TABLE[(n >> 18) & 63];
out += TABLE[(n >> 12) & 63];
out += i + 1 < s.size() ? TABLE[(n >> 6) & 63] : '=';
out += i + 2 < s.size() ? TABLE[n & 63] : '=';
}
std::cout << out << "\n";
return 0;
}
aGVsbG8gcGhw
C# v11.0.100
Console.WriteLine(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("hello php")));
aGVsbG8gcGhw
Go v1.27.4
package main
import (
"encoding/base64"
"fmt"
)
func main() {
fmt.Println(base64.StdEncoding.EncodeToString([]byte("hello php")))
}
aGVsbG8gcGhw
Java v27.0.1
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Main {
public static void main(String[] args) {
byte[] bytes = "hello php".getBytes(StandardCharsets.UTF_8);
System.out.println(Base64.getEncoder().encodeToString(bytes));
}
}
aGVsbG8gcGhw
JavaScript v26.13.1
console.log(Buffer.from("hello php", "utf8").toString("base64"));
aGVsbG8gcGhw
PHP v8.6.0
echo base64_encode("hello php");
aGVsbG8gcGhw
Python v3.15.2
import base64
print(base64.b64encode(b"hello php").decode())
aGVsbG8gcGhw
Rust v1.99.1
fn main() {
let s = b"hello php";
let table = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::new();
for chunk in s.chunks(3) {
let mut n: u32 = (chunk[0] as u32) << 16;
if chunk.len() > 1 {
n |= (chunk[1] as u32) << 8;
}
if chunk.len() > 2 {
n |= chunk[2] as u32;
}
out.push(table[(n >> 18 & 63) as usize] as char);
out.push(table[(n >> 12 & 63) as usize] as char);
out.push(if chunk.len() > 1 { table[(n >> 6 & 63) as usize] as char } else { '=' });
out.push(if chunk.len() > 2 { table[(n & 63) as usize] as char } else { '=' });
}
println!("{}", out);
}
aGVsbG8gcGhw
TypeScript v26.13.1
const text: string = "hello php";
console.log(Buffer.from(text, "utf8").toString("base64"));
aGVsbG8gcGhw
Language differences
C: the standard library has no base64 — this entry hand-writes a minimal implementation (third-party libraries in production).
C++: same as C — no base64 in the standard library, hand-written minimal implementation (third-party libraries in production).
C#: Convert.ToBase64String is a one-liner.
Go: base64.StdEncoding is a one-liner.
Java: Base64.getEncoder is a one-liner; for MIME mail (76-character lines) use getMimeEncoder().
JavaScript: mind the environment split — Node uses Buffer (any input encoding), while browsers expose btoa/atob accepting only Latin1-range strings; Unicode needs a TextEncoder pass first.
PHP: the algorithm itself is identical across all ten languages — each group of 3 bytes maps to 4 characters (alphabet A-Za-z0-9+/), padded with = at the end; base64_encode is a one-liner; on the decode side base64_decode with the second strict argument rejects invalid characters.
Python: base64.b64encode is a one-liner; it takes bytes and returns bytes, hence .decode() back to str; for MIME use base64.encodebytes.
Rust: none built in either; the ecosystem convention is the base64 crate (hand-written here to avoid third-party deps).
TypeScript: same as JavaScript — Node uses Buffer, browsers expose btoa/atob with Latin1-only input.