function BigInteger::fromBytes
Translates a string of bytes containing the binary representation of a BigInteger into a BigInteger.
The input string is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.
If `$signed` is true, the input is assumed to be in two's-complement representation, and the leading bit is interpreted as a sign bit. If `$signed` is false, the input is interpreted as an unsigned number, and the resulting BigInteger will always be positive or zero.
This method can be used to retrieve a number exported by `toBytes()`, as long as the `$signed` flags match.
Parameters
string $value The byte string.:
bool $signed Whether to interpret as a signed number in two's-complement representation with a leading: sign bit.
Throws
NumberFormatException If the string is empty.
1 call to BigInteger::fromBytes()
- BigInteger::randomBits in vendor/
brick/ math/ src/ BigInteger.php - Generates a pseudo-random number in the range 0 to 2^numBits - 1.
File
-
vendor/
brick/ math/ src/ BigInteger.php, line 174
Class
- BigInteger
- An arbitrary-size integer.
Namespace
Brick\MathCode
public static function fromBytes(string $value, bool $signed = true) : BigInteger {
if ($value === '') {
throw new NumberFormatException('The byte string must not be empty.');
}
$twosComplement = false;
if ($signed) {
$x = \ord($value[0]);
if ($twosComplement = $x >= 0x80) {
$value = ~$value;
}
}
$number = self::fromBase(\bin2hex($value), 16);
if ($twosComplement) {
return $number->plus(1)
->negated();
}
return $number;
}