dechex

dechex(int $number): string

十进制数转换为十六进制字符串。

C C23

C23
#include <stdio.h>

int main(void) {
    printf("%x\n%x\n%x\n", 255, 2026, 4095);
    return 0;
}
输出
ff
7ea
fff

C++ C++26

C++26
#include <iostream>
int main() {
    std::cout << std::hex << 255 << "\n" << 2026 << "\n" << 4095 << "\n";
    return 0;
}
输出
ff
7ea
fff

C# v11.0.100

v11.0.100
Console.WriteLine(255.ToString("x"));
Console.WriteLine(2026.ToString("x"));
Console.WriteLine(4095.ToString("x"));
输出
ff
7ea
fff

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    fmt.Printf("%x\n%x\n%x\n", 255, 2026, 4095)
}
输出
ff
7ea
fff

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.toHexString(255));
        System.out.println(Integer.toHexString(2026));
        System.out.println(Integer.toHexString(4095));
    }
}
输出
ff
7ea
fff

JavaScript v26.13.1

v26.13.1
console.log((255).toString(16));
console.log((2026).toString(16));
console.log((4095).toString(16));
输出
ff
7ea
fff

PHP v8.6.0

v8.6.0
echo dechex(255), "\n", dechex(2026), "\n", dechex(4095);
输出
ff
7ea
fff

Python v3.15.2

v3.15.2
print(format(255, "x"))
print(format(2026, "x"))
print(format(4095, "x"))
输出
ff
7ea
fff

Rust v1.99.1

v1.99.1
fn main() {
    println!("{:x}\n{:x}\n{:x}", 255, 2026, 4095);
}
输出
ff
7ea
fff

TypeScript v26.13.1

v26.13.1
const n: number = 255;
console.log(n.toString(16));
console.log((2026).toString(16));
console.log((4095).toString(16));
输出
ff
7ea
fff

语言差异说明

C:%x;负数按位补码展开。

C++:std::hex;负数按位补码展开。

C#:"x" 格式;负数按 32 位补码。

Go:%x;负数带符号前缀;大写、0x 前缀、补零用各自的格式化开关(%X、%08x,C 系同理)。

Java:Integer.toHexString;负数固定按 32 位无符号。

JavaScript:(255).toString(16);负数直接输出 - 开头(要补码形需 >>> 0 先转无符号 32 位)。

PHP:十进制整数→十六进制字符串,各语言输出全一致:小写、无 0x 前缀、无补零——dechex;负数按位补码展开(64 位平台展开 64 位,dechex(-1) 为 16 个 f)。

Python:format(n, 'x');负数带符号前缀(:x 输出 - 开头)。

Rust:{:x};负数按位补码展开。

TypeScript:同 JavaScript,(255).toString(16)。