function BigInteger::fromArbitraryBase
Parses a string containing an integer in an arbitrary base, using a custom alphabet.
Because this method accepts an alphabet with any character, including dash, it does not handle negative numbers.
@psalm-pure
Parameters
string $number The number to parse.:
string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.:
Throws
NumberFormatException If the given number is empty or contains invalid chars for the given alphabet.
\InvalidArgumentException If the alphabet does not contain at least 2 chars.
File
-
vendor/
brick/ math/ src/ BigInteger.php, line 134
Class
- BigInteger
- An arbitrary-size integer.
Namespace
Brick\MathCode
public static function fromArbitraryBase(string $number, string $alphabet) : BigInteger {
if ($number === '') {
throw new NumberFormatException('The number cannot be empty.');
}
$base = \strlen($alphabet);
if ($base < 2) {
throw new \InvalidArgumentException('The alphabet must contain at least 2 chars.');
}
$pattern = '/[^' . \preg_quote($alphabet, '/') . ']/';
if (\preg_match($pattern, $number, $matches) === 1) {
throw NumberFormatException::charNotInAlphabet($matches[0]);
}
$number = Calculator::get()->fromArbitraryBase($number, $alphabet, $base);
return new BigInteger($number);
}