Skip to content

Core\Math::fromBase

Core\Math::fromBase(string $s, uint $base): intCore\Math::fromBase(
    string $s,
    uint $base,
): int

Core\Math::fromBase reads a number written in another base and returns it as an int. The base is from 2 to 36. Base 2 is binary, base 16 is hexadecimal, and digits above 9 are the letters a to z. Upper and lower case letters are the same digit.

A leading - makes the number negative. Every other character must be a digit of the base. A prefix such as 0x, a space or an empty string throws a RuntimeError. A number too large for an int throws an ArithmeticError.

Core\Math::toBase does the opposite. It writes an int in a base.

The examples below show binary and hexadecimal numbers, then text that is not a number, then how to read a colour code such as #FF8800.

Parameters

  • $sstringThe digits. A leading - is allowed. A prefix such as 0x, spaces and separators are not allowed. Letters can be upper or lower case.
  • $baseuintThe base, from 2 to 36. Digits above 9 are the letters a to z.

Return value

Returns int. The number as an int. Core\Math::toBase writes it back.

Errors

  • RuntimeErrorWhen $base is outside 2 to 36, when $s has no digits, or when a character of $s is not a digit of the base.
  • ArithmeticErrorWhen the number is too large for an int.

Examples

Binary and hex

<?nvs
// Shows how to read numbers written in binary, octal and hexadecimal.
// This prints 5, 493 and 255.
echo Core\Math::fromBase('101', 2), "\n";
echo Core\Math::fromBase('755', 8), "\n";
echo Core\Math::fromBase('ff', 16), "\n";
// Upper case letters are the same digits. This prints 255 again.
echo Core\Math::fromBase('FF', 16), "\n";
// A leading `-` makes the number negative. This prints -26.
echo Core\Math::fromBase('-1a', 16), "\n";
// `Core\Math::toBase` writes the number back. This prints 1a.
echo Core\Math::toBase(26, 16), "\n";
Output
5
493
255
255
-26
1a

Text that is not a number

<?nvs
// Shows what happens when the text is not a number in the base.
// `y` is not a hexadecimal digit, so each call throws a `RuntimeError`.
// A prefix such as `0x` and an empty string also throw.
array<string> $inputs = ['beefy', '0xff', ''];
foreach ($inputs as string $input) {
try {
echo Core\Math::fromBase($input, 16), "\n";
} catch (RuntimeError $e) {
echo "not a number: \"", $input, "\"\n";
}
}
// This number is too large for an `int`, so the call throws an `ArithmeticError`.
try {
echo Core\Math::fromBase('ffffffffffffffff', 16), "\n";
} catch (ArithmeticError $overflow) {
echo "too large\n";
}
Output
not a number: "beefy"
not a number: "0xff"
not a number: ""
too large

Colour code

<?nvs
// Shows how to read the red, green and blue parts of a colour code such as `#FF8800`.
class Colour
{
// Returns one part of the colour code as a number from 0 to 255.
// `$start` is the position of the part's two digits, after the `#`.
public static function part(string $code, int $start): int
{
return Core\Math::fromBase(Core\Str::slice($code, $start, 2), 16);
}
}
string $code = '#FF8800';
// This prints red 255, green 136, blue 0.
echo 'red ', Colour::part($code, 1), ', green ', Colour::part($code, 3),
', blue ', Colour::part($code, 5), "\n";
Output
red 255, green 136, blue 0