base_convert
base_convert(string $num, int $from_base, int $to_base): string
Arbitrary-base string conversion (2-36 both ways); large numbers lose precision through the internal float (manual warning, verified) - use BCMath/GMP for exactness.
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
/* strtol(p, base) parses base 2-36; printing back in a non-standard
base has no stdlib helper - write the digits out by hand */
printf("%ld\n", strtol("ff", NULL, 16));
printf("%ld\n", strtol("1010", NULL, 2));
printf("%ld\n", strtol("z", NULL, 36));
unsigned long v = 255;
char buf[32];
int pos = 0;
do {
int d = (int)(v % 16);
buf[pos++] = d < 10 ? '0' + d : 'a' + d - 10;
v /= 16;
} while (v > 0);
for (int i = pos - 1; i >= 0; i--) putchar(buf[i]);
putchar('\n');
return 0;
}
255 10 35 ff
C++ C++26
#include <charconv>
#include <iostream>
#include <string>
int main() {
// std::from_chars parses base 2-36 (locale-free); to_chars formats only
// fixed radixes (16/10/8/2), other bases need a hand loop
const std::string hex = "ff", bin = "1010", b36 = "z";
int v16 = 0, v2 = 0, v36 = 0;
std::from_chars(hex.data(), hex.data() + hex.size(), v16, 16);
std::from_chars(bin.data(), bin.data() + bin.size(), v2, 2);
std::from_chars(b36.data(), b36.data() + b36.size(), v36, 36);
std::cout << v16 << " " << v2 << " " << v36 << " ";
char buf[8];
auto res = std::to_chars(buf, buf + 8, 255, 16);
std::cout << std::string(buf, res.ptr) << "\n";
return 0;
}
255 10 35 ff
C# v11.0.100
using System.Numerics;
// Convert.ToInt32/ToString support radix 2/8/10/16 only (not 2-36!)
Console.WriteLine(Convert.ToInt32("ff", 16));
Console.WriteLine(Convert.ToInt32("1010", 2));
Console.WriteLine(Convert.ToString(255, 16));
// Arbitrary base 2-36: fold digits by hand (BigInteger keeps it exact)
int ParseDigit(char ch) => ch >= '0' && ch <= '9' ? ch - '0' : char.ToLower(ch) - 'a' + 10;
BigInteger big = 0;
foreach (char ch in new string('z', 16))
{
big = big * 36 + ParseDigit(ch);
}
Console.WriteLine(big);
Console.WriteLine(big == BigInteger.Pow(36, 16) - 1);
Console.WriteLine(ParseDigit('z'));
255 10 ff 7958661109946400884391935 True 35
Go v1.27.4
package main
import (
"fmt"
"math/big"
"strconv"
"strings"
)
func main() {
// strconv.ParseInt/FormatInt: native base 2-36 both ways
n, _ := strconv.ParseInt("ff", 16, 64)
fmt.Println(n)
v2, _ := strconv.ParseInt("1010", 2, 64)
fmt.Println(v2)
v36, _ := strconv.ParseInt("z", 36, 64)
fmt.Println(v36)
fmt.Println(strconv.FormatInt(255, 16))
// int64 caps at 2^63-1: math/big is the exact fallback
b := new(big.Int)
b.SetString(strings.Repeat("z", 16), 36)
ref := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(36), big.NewInt(16), nil), big.NewInt(1))
fmt.Println(b, b.Cmp(ref) == 0)
}
255 10 35 ff 7958661109946400884391935 true
Java v27.0.1
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
// Integer.parseInt / Integer.toString: native base 2-36 conversions
System.out.println(Integer.parseInt("ff", 16));
System.out.println(Integer.parseInt("1010", 2));
System.out.println(Integer.parseInt("z", 36));
System.out.println(Integer.toString(255, 16));
// int caps at 2^31-1: BigInteger is the exact fallback (radix 2-36)
BigInteger b = new BigInteger("zzzzzzzzzzzzzzzz", 36);
System.out.println(b);
System.out.println(b.compareTo(BigInteger.valueOf(36).pow(16).subtract(BigInteger.ONE)) == 0);
}
}
255 10 35 ff 7958661109946400884391935 true
JavaScript v26.13.1
// parseInt(s, radix) + n.toString(radix): native both ways, radix 2-36
console.log(parseInt("ff", 16), parseInt("1010", 2), parseInt("z", 36), (255).toString(16));
// Beyond 2^53 Number loses integer precision: BigInt stays exact,
// the Number path really is off by one here
const exact = [..."z".repeat(16)].reduce((acc, ch) => acc * 36n + BigInt(parseInt(ch, 36)), 0n);
console.log(exact.toString());
console.log(exact === 36n ** 16n - 1n);
console.log(BigInt(parseInt("z".repeat(16), 36)).toString());
255 10 35 ff 7958661109946400884391935 true 7958661109946400884391936
PHP v8.6.0
// Base 2-36 conversions both ways; input and output are strings
var_dump(base_convert('ff', 16, 10));
var_dump(base_convert('1010', 2, 10));
var_dump(base_convert('z', 36, 10));
var_dump(base_convert('255', 10, 16));
// Manual warning: big numbers may lose precision through the internal float
// - cross-check against the exact BCMath value
$exact = bcsub(bcpow('36', '16'), '1');
$converted = base_convert(str_repeat('z', 16), 36, 10);
echo $exact, "\n";
echo $converted, "\n";
var_dump($exact === $converted);
string(3) "255" string(2) "10" string(2) "35" string(2) "ff" 7958661109946400884391935 7958661109946402688066886 bool(false)
Python v3.15.2
# int(s, base) parses base 2-36; format()/str prints back
print(int("ff", 16))
print(int("1010", 2))
print(int("z", 36))
print(format(255, "x"))
# Arbitrary precision is native: no float round-trip, ever exact
print(int("z" * 16, 36))
print(int("z" * 16, 36) == 36**16 - 1)
255 10 35 ff 7958661109946400884391935 True
Rust v1.99.1
fn main() {
// i64::from_str_radix parses base 2-36; {:x} formats hex on output
println!("{}", i64::from_str_radix("ff", 16).unwrap());
println!("{}", i64::from_str_radix("1010", 2).unwrap());
println!("{}", i64::from_str_radix("z", 36).unwrap());
println!("{:x}", 255i64);
// i64 caps at 2^63-1: u128 covers this case, bigger needs a crate
println!("{}", u128::from_str_radix("zzzzzzzzzzzzzzzz", 36).unwrap());
println!("{}", u128::from_str_radix("zzzzzzzzzzzzzzzz", 36).unwrap() == 36u128.pow(16) - 1);
}
255 10 35 ff 7958661109946400884391935 true
TypeScript v26.13.1
// parseInt(s, radix) + n.toString(radix): native both ways, radix 2-36
console.log(parseInt("ff", 16), parseInt("1010", 2), parseInt("z", 36), (255).toString(16));
// Beyond 2^53 Number loses integer precision: BigInt stays exact
const exact: bigint = [..."z".repeat(16)].reduce(
(acc: bigint, ch: string) => acc * 36n + BigInt(parseInt(ch, 36)),
0n,
);
console.log(exact.toString());
console.log(exact === 36n ** 16n - 1n);
255 10 35 ff 7958661109946400884391935 true
Language differences
C: strtol(p, base) parses base 2-36 (errno/pointer report errors); non-decimal/hex output has no stdlib helper - divide-by-base digit extraction by hand (itoa is not standard).
C++: C++17 std::from_chars parses base 2-36 (locale-free, allocation-free); to_chars supports only 2/8/10/16, arbitrary-base output is a hand loop.
C#: Convert.ToInt32/ToString support only base 2/8/10/16 (passing 36 throws ArgumentException outright - the manual states base must be 2/8/10/16); arbitrary base 2-36 needs a hand digit fold plus BigInteger.
Go: strconv.ParseInt/FormatInt are natively 2-36 both ways; beyond int64, math/big (SetString(s, base)) takes over exactly.
Java: Integer.parseInt/toString(s, radix) natively 2-36; beyond int use Long, then new BigInteger(s, radix) (the constructor supports 2-36).
JavaScript: parseInt(s, radix) + n.toString(radix) are native 2-36 both ways; precision is lost beyond 2^53 (verified: the Number path is off by one for 36^16-1), BigInt is exact but toString only supports 10/16 (arbitrary bases need a hand fold).
PHP: base_convert is 2-36 both ways, strings in and out; manual Warning: large numbers may lose precision through the internal float - verified (exact 36^16-1 is 7958661109946400884391935, base_convert returns 7958661109946402688066886; this entry cross-checks with BCMath bcpow+bcsub); the manual also states invalid characters produce a deprecation notice since PHP 7.4.0 (chars silently ignored). Use BCMath/GMP for exact big-number work.
Python: int(s, base) parses base 2-36; format(n, "x")/str output supports fixed bases only (2/8/10/16), arbitrary bases are hand-written; arbitrary precision is native (no float round-trip, always exact).
Rust: i64::from_str_radix(s, radix) parses base 2-36 (Result-based errors); output {:x}/{:b} supports fixed bases only, arbitrary bases need hand code (or a third-party crate).
TypeScript: same as JavaScript (native parseInt/toString, BigInt as the exact fallback).