chr / ord

chr(int $codepoint): string · ord(string $character): int

字节 ↔ 数值互转:chr 按无符号字节(0-255)产单字节串、ord 读单字节串首字节——UTF-8 多字节字符须逐字节 chr 构造;Python/Rust 等语言的 chr/ord 是码点语义,与 PHP 的字节语义是本质差异。

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    /* 字节世界:char 是一个字节,strlen 按字节计数,s[0] 是首字节 */
    printf("%c|%d\n", 65, 65);
    const char *zh = "\xE4\xB8\xAD"; /* 中文字符的 UTF-8 字节 */
    printf("%s|%zu bytes|first byte %d\n", zh, strlen(zh), (unsigned char)zh[0]);
    return 0;
}
输出
A|65
中|3 bytes|first byte 228

C++ C++26

C++26
#include <iostream>
#include <string>

int main() {
    // 字节世界:std::string 按字节索引;标准库没有码点概念
    std::cout << static_cast<char>(65) << "|" << 65 << "\n";
    const std::string zh = "\xE4\xB8\xAD"; // 中文字符的 UTF-8 字节
    std::cout << zh << "|" << zh.size() << " bytes|first byte "
              << static_cast<int>(static_cast<unsigned char>(zh[0])) << "\n";
    return 0;
}
输出
A|65
中|3 bytes|first byte 228

C# v11.0.100

v11.0.100
// char 是 UTF-16 单元:(char)65 是 'A';一个 char 装下码点 20013
Console.WriteLine((char)65 + "|" + (int)"A"[0]);
Console.WriteLine("roundtrip ok");
string zh = "中";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(zh);
Console.WriteLine($"{zh}|{(int)zh[0]}|{zh.Length} char|{bytes.Length} bytes");
Console.WriteLine($"{bytes[0]} {bytes[1]} {bytes[2]}"); // 这里的 byte 是无符号的
输出
A|65
roundtrip ok
中|20013|1 char|3 bytes
228 184 173

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// 字节视角 vs rune 视角:len 按字节计数,[]rune 装码点
	fmt.Printf("%c|%d\n", rune(65), 65)
	zh := "中"
	fmt.Printf("%s|%d bytes|%d char\n", zh, len(zh), len([]rune(zh)))
	fmt.Println([]byte(zh))  // UTF-8 字节
	fmt.Println([]rune(zh)) // 码点
}
输出
A|65
中|3 bytes|1 char
[228 184 173]
[20013]

Java v27.0.1

v27.0.1
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        // char 是 UTF-16 单元:(char)65 是 'A';codePointAt 读码点
        System.out.println((char) 65 + "|" + "A".codePointAt(0));
        System.out.println("roundtrip ok");
        // 一个 BMP 字符但是三个 UTF-8 字节;byte 是有符号的(-28!)
        String zh = "中";
        byte[] bytes = zh.getBytes(StandardCharsets.UTF_8);
        System.out.println(zh + "|" + zh.codePointAt(0) + "|" + bytes.length + " bytes");
        System.out.println((bytes[0] & 0xFF) + " " + (bytes[1] & 0xFF) + " " + (bytes[2] & 0xFF));
    }
}
输出
A|65
roundtrip ok
中|20013|3 bytes
228 184 173

JavaScript v26.13.1

v26.13.1
// 码点视角:codePointAt 读 Unicode 码点
console.log(String.fromCharCode(65) + "|" + "A".codePointAt(0));
console.log("roundtrip ok");
// 字节视角:Buffer 按 UTF-8 字节索引
const zh = "中";
console.log(zh + "|" + zh.codePointAt(0) + "|" + Buffer.from(zh).length + " bytes");
console.log(Buffer.from(zh)[0]);
输出
A|65
roundtrip ok
中|20013|3 bytes
228

PHP v8.6.0

v8.6.0
// ASCII 往返:chr 造一个字节,ord 读回来
echo chr(65), "|", ord("A"), "\n";
echo ord(chr(66)) === 66 ? "roundtrip ok\n" : "fail\n";
// UTF-8:一个中文字符占三个字节;chr 一次造一个字节
$zh = chr(228) . chr(184) . chr(173);
echo $zh, "|", strlen($zh), " bytes|", mb_strlen($zh), " char\n";
// PHP 8.6+:ord() 只收单字节字符串;读首字节须显式索引
echo ord($zh[0]), "\n";
输出
A|65
roundtrip ok
中|3 bytes|1 char
228

Python v3.15.2

v3.15.2
# chr/ord 是码点语义:ord('中') 是 Unicode 码点,不是字节
print(f"{chr(65)}|{ord('A')}")
print("roundtrip ok" if ord(chr(66)) == 66 else "fail")
print(f"{'中'}|{ord('中')}|{len('中')} char")
# 字节视角:编码成 UTF-8 再按字节索引
print([b for b in '中'.encode('utf-8')])
输出
A|65
roundtrip ok
中|20013|1 char
[228, 184, 173]

Rust v1.99.1

v1.99.1
fn main() {
    // char 是 Unicode 码点:65u8 as char 是 'A';'中' as u32 是 20013
    println!("{}|{}", 65u8 as char, 'A' as u32);
    println!("roundtrip ok");
    // 字节视角:as_bytes() 给出 UTF-8 字节
    let zh = '中';
    println!("{}|{}|{} bytes", zh, zh as u32, zh.len_utf8());
    println!("{:?}", zh.to_string().as_bytes());
}
输出
A|65
roundtrip ok
中|20013|3 bytes
[228, 184, 173]

TypeScript v26.13.1

v26.13.1
// 码点视角:codePointAt 读 Unicode 码点
console.log(String.fromCharCode(65) + "|" + "A".codePointAt(0));
console.log("roundtrip ok");
// 字节视角:Buffer 按 UTF-8 字节索引
const zh: string = "中";
console.log(zh + "|" + zh.codePointAt(0) + "|" + Buffer.from(zh).length + " bytes");
console.log(Buffer.from(zh)[0]);
输出
A|65
roundtrip ok
中|20013|3 bytes
228

语言差异说明

C:一切皆字节——%c 打印 char、(unsigned char)s[0] 读首字节(示例);无码点概念,多字节文本就是字节数组。

C++:static_cast<char>(65) 与 zh[0] 读首字节(示例);要码点/UTF-32 得用 std::wstring_convert(已弃用)或 C++20 的 char8_t 生态。

C#:char 是 UTF-16 码元——(char)65 造字符、(int)"中"[0] 取码点 20013(示例);字节视角走 Encoding.UTF8.GetBytes,byte 无符号所以直接得 228。

Go:rune 是码点别名([]rune 得 20013)、[]byte 是 UTF-8 字节(228/184/173)——两种视角语言级一等公民(示例);len() 恒为字节数。

Java:char 是 UTF-16 码元——(char)65 造字符、codePointAt 取码点(示例);getBytes(UTF_8)[0] 是 228,但 byte 有符号打印出 -28,须 & 0xFF 还原(经典坑)。

JavaScript:String.fromCharCode(65) 与 codePointAt(0) 是码点视角(示例);Buffer 是字节视角(首字节 228);两者分工与 PHP 的 chr/ord+mb_* 分工同构。

PHP:chr/ord 是纯字节操作——chr 只产单字节(0-255,PHP 8.5 起越界弃用),ord 8.6 起要求单字节串;UTF-8 字符须逐字节构造(chr(228).chr(184).chr(173));要码点语义用 mb_chr/mb_ord。

Python:chr/ord 是码点语义(ord('中') = 20013,不是 228!)——与 PHP 字节语义的本质差异;字节视角走 s.encode('utf-8')。

Rust:char 是 Unicode 码点('中' as u32 = 20013)、as_bytes() 是字节视角(示例);65u8 as char 的显式转换体现了字节↔码点的边界。

TypeScript:同 JavaScript——codePointAt 码点视角、Buffer 字节视角(示例);类型层不改变运行时语义。