hexdec
hexdec(string $hex_string): int|float
Converts a hexadecimal string to a decimal number.
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *hexes[] = {"ff", "7ea", "fff"};
for (int i = 0; i < 3; i++) {
printf("%ld\n", strtol(hexes[i], NULL, 16));
}
return 0;
}
255 2026 4095
C++ C++26
#include <iostream>
#include <string>
int main() {
for (const std::string s : {"ff", "7ea", "fff"}) {
std::cout << std::stoi(s, nullptr, 16) << "\n";
}
return 0;
}
255 2026 4095
C# v11.0.100
foreach (var s in new[] { "ff", "7ea", "fff" }) {
Console.WriteLine(Convert.ToInt32(s, 16));
}
255 2026 4095
Go v1.27.4
package main
import (
"fmt"
"strconv"
)
func main() {
for _, s := range []string{"ff", "7ea", "fff"} {
n, err := strconv.ParseInt(s, 16, 64)
if err != nil {
panic(err)
}
fmt.Println(n)
}
}
255 2026 4095
Java v27.0.1
public class Main {
public static void main(String[] args) {
for (String s : new String[]{"ff", "7ea", "fff"}) {
System.out.println(Integer.parseInt(s, 16));
}
}
}
255 2026 4095
JavaScript v26.13.1
console.log(parseInt("ff", 16));
console.log(parseInt("7ea", 16));
console.log(parseInt("fff", 16));
255 2026 4095
PHP v8.6.0
echo hexdec("ff"), "\n", hexdec("7ea"), "\n", hexdec("fff");
255 2026 4095
Python v3.15.2
print(int("ff", 16))
print(int("7ea", 16))
print(int("fff", 16))
255 2026 4095
Rust v1.99.1
fn main() {
for s in ["ff", "7ea", "fff"] {
let n = i64::from_str_radix(s, 16).unwrap();
println!("{n}");
}
}
255 2026 4095
TypeScript v26.13.1
const hex: string = "7ea";
console.log(parseInt("ff", 16));
console.log(parseInt(hex, 16));
console.log(parseInt("fff", 16));
255 2026 4095
Language differences
C: strtol(s, NULL, 16); stops at the first invalid character (no error; inspect endptr); accepts the 0x prefix.
C++: std::stoi(s, nullptr, 16); prefix parsing (reads up to the first invalid character); accepts the 0x prefix.
C#: Convert.ToInt32(s, 16); throws strictly; rejects the 0x prefix.
Go: strconv.ParseInt(s, 16, 64); throws strictly; base=16 rejects the 0x prefix (use base=0 for prefix parsing).
Java: Integer.parseInt(s, 16); throws strictly (NumberFormatException); rejects the 0x prefix; 32-bit only (use Long.parseLong for big values).
JavaScript: parseInt(s, 16); prefix parsing; accepts the 0x prefix.
PHP: hex string → decimal integer, and all examples agree — via hexdec; attitudes toward invalid characters differ the most: it skips non-hex characters and parses the rest (hexdec("0xFF") still yields 255 — garbage in, fruit out); range: returns float once the result exceeds the int ceiling.
Python: int(s, 16); throws strictly (ValueError); accepts the 0x prefix.
Rust: i64::from_str_radix(s, 16); throws strictly (Err); rejects the 0x prefix.
TypeScript: same as JavaScript — parseInt(s, 16).