base64_encode

base64_encode(string $string): string

使用 MIME base64 对数据进行编码(输出不含换行)。

C C23

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

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

v11.0.100
Console.WriteLine(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("hello php")));
输出
aGVsbG8gcGhw

Go v1.27.4

v1.27.4
package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    fmt.Println(base64.StdEncoding.EncodeToString([]byte("hello php")))
}
输出
aGVsbG8gcGhw

Java v27.0.1

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

v26.13.1
console.log(Buffer.from("hello php", "utf8").toString("base64"));
输出
aGVsbG8gcGhw

PHP v8.6.0

v8.6.0
echo base64_encode("hello php");
输出
aGVsbG8gcGhw

Python v3.15.2

v3.15.2
import base64

print(base64.b64encode(b"hello php").decode())
输出
aGVsbG8gcGhw

Rust v1.99.1

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

v26.13.1
const text: string = "hello php";
console.log(Buffer.from(text, "utf8").toString("base64"));
输出
aGVsbG8gcGhw

语言差异说明

C:标准库没有 base64,本条目手写最小实现(生产用第三方库)。

C++:同 C,标准库没有 base64,手写最小实现(生产用第三方库)。

C#:Convert.ToBase64String 一行搞定。

Go:base64.StdEncoding 一行搞定。

Java:Base64.getEncoder 一行搞定;MIME 场景(邮件按 76 字符插换行)用 getMimeEncoder()。

JavaScript:注意环境分野——Node 用 Buffer(支持任意编码输入),浏览器是 btoa/atob 且只接受 Latin1 范围字符串,处理 Unicode 需先 TextEncoder 转字节。

PHP:算法本身 10 门语言输出完全一致:每 3 字节一组映射为 4 个字符(字母表 A-Za-z0-9+/),末尾不足补 =;base64_encode 一行搞定;解码侧 base64_decode 严格模式第二参数传 true 可拒绝非法字符。

Python:base64.b64encode 一行搞定,收 bytes 返回 bytes,.decode() 转回字符串;MIME 场景用 base64.encodebytes。

Rust:同样无内置,生态惯例是 base64 crate(本条目为免第三方依赖手写)。

TypeScript:同 JavaScript,Node 用 Buffer、浏览器 btoa/atob 只接受 Latin1。