bindec / octdec / decbin / dechex / hexdec (fixed-base quick reference)

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

Quick reference for the fixed-base conversions between binary/octal/hexadecimal and decimal: bindec/octdec/hexdec parse to decimal, decbin/dechex format from decimal. For arbitrary bases (2-36) see the base_convert entry; the single-function deep dives live in the dechex/hexdec entries.

C C23

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

int main(void) {
    /* strtol(p, NULL, base) parses any base 2-36 */
    printf("%ld\n", strtol("10101010", NULL, 2));
    printf("%ld\n", strtol("755", NULL, 8));
    /* printing binary has no stdlib helper: hand-write divide-by-2 loop */
    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 formats hex directly */
    printf("%ld\n", strtol("deadbeef", NULL, 16));
    printf("%lx\n", 3735928559UL);             /* round trip back to hex */
    return 0;
}
Output
170
493
10101010
1ed
3735928559
deadbeef

C++ C++26

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

int main() {
    // std::strtol parses any base 2-36; binary printing is hand-written
    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;
}
Output
170
493
10101010
1ed
3735928559
deadbeef

C# v11.0.100

v11.0.100
// Convert covers the whole family: ToInt64 parses, ToString formats
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)); // needs Int64: overflows int
Console.WriteLine(Convert.ToString(3735928559L, 16));
Output
170
493
10101010
1ed
3735928559
deadbeef

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strconv"
)

func main() {
	// strconv.ParseInt / FormatInt take the base explicitly
	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))
}
Output
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 take the radix explicitly
        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)); // needs long: overflows int
        System.out.println(Long.toString(3735928559L, 16));
    }
}
Output
170
493
10101010
1ed
3735928559
deadbeef

JavaScript v26.13.1

v26.13.1
// parseInt parses with a radix; Number.prototype.toString formats with one
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)); // exact: still below 2**53
Output
170
493
10101010
1ed
3735928559
deadbeef

PHP v8.6.0

v8.6.0
// five one-way bridges between decimal and the fixed bases 2/8/16
echo bindec("10101010"), "\n";  // binary string -> decimal
echo octdec("755"), "\n";       // octal string -> decimal
echo decbin(170), "\n";         // decimal -> binary string
echo dechex(493), "\n";         // decimal -> hex string
echo hexdec("deadbeef"), "\n";  // hex string -> decimal
echo dechex(3735928559), "\n";  // round trip back to hex
Output
170
493
10101010
1ed
3735928559
deadbeef

Python v3.15.2

v3.15.2
# int(s, base) parses; bin()/oct()/hex() format (0b/0o/0x prefixes sliced off)
print(int("10101010", 2))
print(int("755", 8))
print(bin(170)[2:])
print(format(493, "x"))          # or hex(493)[2:]
print(int("deadbeef", 16))
print(format(3735928559, "x"))
Output
170
493
10101010
1ed
3735928559
deadbeef

Rust v1.99.1

v1.99.1
fn main() {
    // i64::from_str_radix parses; {:b}/{:x} format
    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);
}
Output
170
493
10101010
1ed
3735928559
deadbeef

TypeScript v26.13.1

v26.13.1
// same as JavaScript, with the radix typed where it helps
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)); // exact: still below Number.MAX_SAFE_INTEGER
Output
170
493
10101010
1ed
3735928559
deadbeef

Language differences

Positioning: a quick-reference family for the fixed-base pairs binary/octal/hexadecimal <-> decimal — bindec/octdec/hexdec parse in (string to decimal), decbin/dechex format out (decimal to string); arbitrary bases (2-36) live in the base_convert entry, and the single-function deep dives are the dechex/hexdec entries.

All five PHP functions are built in and mirror each other (bindec(decbin($n)) === $n); the sibling decoct (decimal to octal) shares the same shape and is not demoed here.

Parsing side: C strtol(s, NULL, base) (<stdlib.h> explicit, bases 2-36), Go strconv.ParseInt, Java Long.parseLong, C# Convert.ToInt64, Rust i64::from_str_radix (returns Result), JS/TS parseInt, Python int(s, base).

Formatting side: C %x/%lx plus a hand-written divide-by-base loop for binary (same trick as the base_convert entry), Go strconv.FormatInt, Java Integer.toString(n, radix), C# Convert.ToString, Rust {:b}/{:x}, JS/TS n.toString(radix), Python bin()/format(n, 'x').

Case convention: hex output is lowercase everywhere, with no 0x prefix and no zero padding (same conclusion as the dechex entry); uppercase, the 0x prefix and padding are per-language format switches.

Boundary design: 0xdeadbeef = 3735928559 fits in uint32 and sits far below the JS safe-integer limit (2^53), so all ten languages stay exact; beyond 2^53 JS needs BigInt and PHP hexdec beyond int64 returns a float — the large-number precision-loss lesson lives in the base_convert entry.

Invalid-input semantics differ: PHP 8.1+ raises a Deprecated notice for illegal digits and truncates at the prefix (measured: bindec("12") gives 1), Rust/Go return Err (the example's unwrap would panic), C# throws FormatException, and C strtol stops at the first illegal character — validate before parsing in production code.

Negatives and widths: strtol accepts a leading -; formatting of negatives varies by language (bit-pattern expansion vs - prefix vs exception), tabulated in the dechex entry; this demo stays non-negative to keep all ten outputs identical.