bindec / octdec / decbin / dechex / hexdec

bindec(string $binary_string): int|float — octdec(string $octal_string): int|float — decbin(int $num): string — dechex(int $num): string — hexdec(string $hex_string): int|float

二进制/八进制/十六进制与十进制的「两定点」速查族:bindec/octdec/hexdec 入库(→十进制),decbin/dechex 出库(十进制→)。任意进制(2-36)转换见 base_convert 条目,单函数深描见 dechex/hexdec 单条。

C C23

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

int main(void) {
    /* strtol(p, NULL, base) 解析任意 2-36 进制 */
    printf("%ld\n", strtol("10101010", NULL, 2));
    printf("%ld\n", strtol("755", NULL, 8));
    /* 二进制输出无标准库助手:手写除 2 取余循环 */
    unsigned long v = 170;
    char buf[64];
    int pos = 0;
    do {
        buf[pos++] = (char)('0' + v % 2);
        v /= 2;
    } while (v > 0);
    while (pos > 0) putchar(buf[--pos]);
    putchar('\n');
    printf("%lx\n", 493UL);                    /* 十六进制用 %x 直接格式化 */
    printf("%ld\n", strtol("deadbeef", NULL, 16));
    printf("%lx\n", 3735928559UL);             /* 往返回十六进制 */
    return 0;
}
输出
170
493
10101010
1ed
3735928559
deadbeef

C++ C++26

C++26
#include <cstdio>
#include <cstdlib>

int main() {
    // std::strtol 解析任意 2-36 进制;二进制输出手写
    std::printf("%ld\n", std::strtol("10101010", nullptr, 2));
    std::printf("%ld\n", std::strtol("755", nullptr, 8));
    unsigned long v = 170;
    char buf[64];
    int pos = 0;
    do {
        buf[pos++] = (char)('0' + v % 2);
        v /= 2;
    } while (v > 0);
    while (pos > 0) std::putchar(buf[--pos]);
    std::putchar('\n');
    std::printf("%lx\n", 493UL);
    std::printf("%ld\n", std::strtol("deadbeef", nullptr, 16));
    std::printf("%lx\n", 3735928559UL);
    return 0;
}
输出
170
493
10101010
1ed
3735928559
deadbeef

C# v11.0.100

v11.0.100
// Convert 覆盖全族:ToInt64 解析、ToString 格式化
Console.WriteLine(Convert.ToInt64("10101010", 2));
Console.WriteLine(Convert.ToInt64("755", 8));
Console.WriteLine(Convert.ToString(170, 2));
Console.WriteLine(Convert.ToString(493, 16));
Console.WriteLine(Convert.ToInt64("deadbeef", 16)); // 需 Int64:超出 int 范围
Console.WriteLine(Convert.ToString(3735928559L, 16));
输出
170
493
10101010
1ed
3735928559
deadbeef

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strconv"
)

func main() {
	// strconv.ParseInt / FormatInt 显式接收进制参数
	v1, _ := strconv.ParseInt("10101010", 2, 64)
	fmt.Println(v1)
	v2, _ := strconv.ParseInt("755", 8, 64)
	fmt.Println(v2)
	fmt.Println(strconv.FormatInt(170, 2))
	fmt.Println(strconv.FormatInt(493, 16))
	v5, _ := strconv.ParseInt("deadbeef", 16, 64)
	fmt.Println(v5)
	fmt.Println(strconv.FormatInt(3735928559, 16))
}
输出
170
493
10101010
1ed
3735928559
deadbeef

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // Long.parseLong / Integer.toString 显式接收 radix 参数
        System.out.println(Long.parseLong("10101010", 2));
        System.out.println(Long.parseLong("755", 8));
        System.out.println(Integer.toString(170, 2));
        System.out.println(Integer.toString(493, 16));
        System.out.println(Long.parseLong("deadbeef", 16)); // 需 long:超出 int 范围
        System.out.println(Long.toString(3735928559L, 16));
    }
}
输出
170
493
10101010
1ed
3735928559
deadbeef

JavaScript v26.13.1

v26.13.1
// parseInt 带基数解析;Number.prototype.toString 带基数格式化
console.log(parseInt("10101010", 2));
console.log(parseInt("755", 8));
console.log((170).toString(2));
console.log((493).toString(16));
console.log(parseInt("deadbeef", 16));
console.log((3735928559).toString(16)); // 精确:仍在 2**53 之下
输出
170
493
10101010
1ed
3735928559
deadbeef

PHP v8.6.0

v8.6.0
// 十进制与固定进制 2/8/16 之间的五座单程桥
echo bindec("10101010"), "\n";  // 二进制字符串 -> 十进制
echo octdec("755"), "\n";       // 八进制字符串 -> 十进制
echo decbin(170), "\n";         // 十进制 -> 二进制字符串
echo dechex(493), "\n";         // 十进制 -> 十六进制字符串
echo hexdec("deadbeef"), "\n";  // 十六进制字符串 -> 十进制
echo dechex(3735928559), "\n";  // 往返回十六进制
输出
170
493
10101010
1ed
3735928559
deadbeef

Python v3.15.2

v3.15.2
# int(s, base) 解析;bin()/oct()/hex() 格式化(切掉 0b/0o/0x 前缀)
print(int("10101010", 2))
print(int("755", 8))
print(bin(170)[2:])
print(format(493, "x"))          # 或 hex(493)[2:]
print(int("deadbeef", 16))
print(format(3735928559, "x"))
输出
170
493
10101010
1ed
3735928559
deadbeef

Rust v1.99.1

v1.99.1
fn main() {
    // i64::from_str_radix 解析;{:b}/{:x} 格式化
    println!("{}", i64::from_str_radix("10101010", 2).unwrap());
    println!("{}", i64::from_str_radix("755", 8).unwrap());
    println!("{:b}", 170);
    println!("{:x}", 493);
    println!("{}", i64::from_str_radix("deadbeef", 16).unwrap());
    println!("{:x}", 3735928559_i64);
}
输出
170
493
10101010
1ed
3735928559
deadbeef

TypeScript v26.13.1

v26.13.1
// 与 JavaScript 相同,关键处补类型标注
const parsed: number = parseInt("10101010", 2);
console.log(parsed);
console.log(parseInt("755", 8));
console.log((170).toString(2));
console.log((493).toString(16));
console.log(parseInt("deadbeef", 16));
console.log((3735928559).toString(16)); // 精确:仍在 Number.MAX_SAFE_INTEGER 之下
输出
170
493
10101010
1ed
3735928559
deadbeef

语言差异说明

定位:二/八/十六 ↔ 十「两定点」速查族——bindec/octdec/hexdec 入库(字符串→十进制),decbin/dechex 出库(十进制→字符串);任意进制(2-36)见 base_convert 条目,dechex/hexdec 单函数深描见各自单条。

PHP 五函数全内置、互为镜像(bindec(decbin($n)) === $n);姊妹函数 decoct(十→八)同源同形,本条未演示。

解析侧对照:C strtol(s, NULL, base)(<stdlib.h> 显式,2-36 通吃)、Go strconv.ParseInt、Java Long.parseLong、C# Convert.ToInt64、Rust i64::from_str_radix(返回 Result)、JS/TS parseInt、Python int(s, base)。

格式化侧对照:C %x/%lx + 二进制手写除基取余循环(同 base_convert 条目的手写套路)、Go strconv.FormatInt、Java Integer.toString(n, radix)、C# Convert.ToString、Rust {:b}/{:x}、JS/TS n.toString(radix)、Python bin()/format(n, 'x')。

大小写约定:全语言十六进制输出小写、无 0x 前缀、不补零(与 dechex 单条结论一致);要大写/0x 前缀/补零走各语言格式开关。

边界设计:0xdeadbeef = 3735928559 在 uint32 内且远在 JS 安全整数(2^53)之下,10 门语言全精确;超出 2^53 的 JS 需 BigInt、超出 int64 的 PHP hexdec 返回 float——大数丢精度教学见 base_convert 条目。

无效输入语义各异:PHP 8.1+ 就非法位发 Deprecated 提示并按前缀截断(bindec("12") 得 1,本机实测)、Rust/Go 返回 Err(示例 unwrap 会 panic)、C# 抛 FormatException、C strtol 停在首个非法字符——生产代码按语言习惯先校验。

负数与位宽:strtol 接受 - 前缀;格式化侧负数行为各语言不同(补码展开 vs - 前缀 vs 抛异常),逐语言对照表见 dechex 单条;本示例全部取非负值以保 10 语言输出一致。