chr / ord

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

Byte-to-number conversions: chr builds a single-byte string from an unsigned byte (0-255) and ord reads the first byte — multi-byte UTF-8 characters must be built byte by byte; chr/ord in Python/Rust are codepoint-based, a fundamental difference from PHP's byte semantics.

C C23

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

int main(void) {
    /* byte world: char is one byte, strlen counts bytes, s[0] is the first byte */
    printf("%c|%d\n", 65, 65);
    const char *zh = "\xE4\xB8\xAD"; /* UTF-8 bytes of the CJK char */
    printf("%s|%zu bytes|first byte %d\n", zh, strlen(zh), (unsigned char)zh[0]);
    return 0;
}
Output
A|65
中|3 bytes|first byte 228

C++ C++26

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

int main() {
    // byte world: std::string indexes bytes; the stdlib has no codepoint concept
    std::cout << static_cast<char>(65) << "|" << 65 << "\n";
    const std::string zh = "\xE4\xB8\xAD"; // UTF-8 bytes of the CJK char
    std::cout << zh << "|" << zh.size() << " bytes|first byte "
              << static_cast<int>(static_cast<unsigned char>(zh[0])) << "\n";
    return 0;
}
Output
A|65
中|3 bytes|first byte 228

C# v11.0.100

v11.0.100
// char is a UTF-16 unit: (char)65 is 'A'; one char holds codepoint 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 is unsigned here
Output
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() {
	// byte view vs rune view: len counts bytes, []rune holds codepoints
	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 bytes
	fmt.Println([]rune(zh)) // codepoints
}
Output
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 is a UTF-16 unit: (char)65 is 'A'; codePointAt reads the codepoint
        System.out.println((char) 65 + "|" + "A".codePointAt(0));
        System.out.println("roundtrip ok");
        // one BMP char but three UTF-8 bytes; byte is signed (-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));
    }
}
Output
A|65
roundtrip ok
中|20013|3 bytes
228 184 173

JavaScript v26.13.1

v26.13.1
// codepoint view: codePointAt reads Unicode codepoints
console.log(String.fromCharCode(65) + "|" + "A".codePointAt(0));
console.log("roundtrip ok");
// byte view: Buffer indexes UTF-8 bytes
const zh = "中";
console.log(zh + "|" + zh.codePointAt(0) + "|" + Buffer.from(zh).length + " bytes");
console.log(Buffer.from(zh)[0]);
Output
A|65
roundtrip ok
中|20013|3 bytes
228

PHP v8.6.0

v8.6.0
// ASCII roundtrip: chr builds a byte, ord reads it back
echo chr(65), "|", ord("A"), "\n";
echo ord(chr(66)) === 66 ? "roundtrip ok\n" : "fail\n";
// UTF-8: a CJK char is three bytes; chr builds one byte at a time
$zh = chr(228) . chr(184) . chr(173);
echo $zh, "|", strlen($zh), " bytes|", mb_strlen($zh), " char\n";
// PHP 8.6+: ord() wants a single-byte string; index to read the first byte
echo ord($zh[0]), "\n";
Output
A|65
roundtrip ok
中|3 bytes|1 char
228

Python v3.15.2

v3.15.2
# chr/ord are codepoint-based: ord of the CJK char gives its codepoint, not a byte
print(f"{chr(65)}|{ord('A')}")
print("roundtrip ok" if ord(chr(66)) == 66 else "fail")
print(f"{'中'}|{ord('中')}|{len('中')} char")
# the byte view: encode to UTF-8 and index bytes
print([b for b in '中'.encode('utf-8')])
Output
A|65
roundtrip ok
中|20013|1 char
[228, 184, 173]

Rust v1.99.1

v1.99.1
fn main() {
    // char is a Unicode codepoint: 65u8 as char is 'A'; the CJK char as u32 is 20013
    println!("{}|{}", 65u8 as char, 'A' as u32);
    println!("roundtrip ok");
    // byte view: as_bytes() gives UTF-8 bytes
    let zh = '中';
    println!("{}|{}|{} bytes", zh, zh as u32, zh.len_utf8());
    println!("{:?}", zh.to_string().as_bytes());
}
Output
A|65
roundtrip ok
中|20013|3 bytes
[228, 184, 173]

TypeScript v26.13.1

v26.13.1
// codepoint view: codePointAt reads Unicode codepoints
console.log(String.fromCharCode(65) + "|" + "A".codePointAt(0));
console.log("roundtrip ok");
// byte view: Buffer indexes UTF-8 bytes
const zh: string = "中";
console.log(zh + "|" + zh.codePointAt(0) + "|" + Buffer.from(zh).length + " bytes");
console.log(Buffer.from(zh)[0]);
Output
A|65
roundtrip ok
中|20013|3 bytes
228

Language differences

C: everything is bytes — %c prints a char and (unsigned char)s[0] reads the first byte (example); no codepoint concept, multi-byte text is just a byte array.

C++: static_cast<char>(65) and zh[0] read the first byte (example); codepoints/UTF-32 would need the deprecated std::wstring_convert or the C++20 char8_t ecosystem.

C#: char is a UTF-16 unit — (char)65 builds a character and (int)"中"[0] reads codepoint 20013 (example); the byte view goes through Encoding.UTF8.GetBytes, and byte is unsigned so 228 comes out directly.

Go: rune is a codepoint alias ([]rune gives 20013) while []byte is UTF-8 bytes (228/184/173) — both views are first-class at the language level (example); len() is always the byte count.

Java: char is a UTF-16 unit — (char)65 builds a character and codePointAt reads the codepoint (example); getBytes(UTF_8)[0] is 228 but byte is signed so it prints -28 and needs & 0xFF (a classic trap).

JavaScript: String.fromCharCode(65) and codePointAt(0) are the codepoint view (example); Buffer is the byte view (first byte 228); the split mirrors PHP's chr/ord plus mb_* division.

PHP: chr/ord are pure byte operations — chr produces only single bytes (0-255; out-of-range deprecated since 8.5) and ord requires a single-byte string since 8.6; UTF-8 characters must be built byte by byte (chr(228).chr(184).chr(173)); for codepoint semantics use mb_chr/mb_ord.

Python: chr/ord are codepoint-based (ord('中') = 20013, not 228!) — the fundamental difference from PHP's byte semantics; the byte view goes through s.encode('utf-8').

Rust: char is a Unicode codepoint ('中' as u32 = 20013) and as_bytes() is the byte view (example); the explicit 65u8 as char conversion marks the byte-codepoint boundary.

TypeScript: same as JavaScript — codePointAt codepoint view, Buffer byte view (example); the type layer does not change runtime semantics.