fromBytes static method
Converts a list of bytes to a BigInt, considering the specified byte order.
The fromBytes method takes a list of bytes and an optional byteOrder
parameter
(defaulting to big endian). It interprets the byte sequence as a non-negative integer,
considering the byte order, and returns the corresponding BigInt representation.
If the byte order is set to little endian, the input bytes are reversed before conversion.
Example Usage:
List<int> bytes = [0x01, 0x02, 0x03, 0x04];
BigInt result = BigIntUtils.fromBytes(bytes); // Result: 16909060
Parameters:
bytes
: The list of bytes to be converted to a BigInt.byteOrder
: The byte order to interpret the byte sequence (default is big endian). Returns: The BigInt representation of the input byte sequence.
Implementation
static BigInt fromBytes(List<int> bytes,
{Endian byteOrder = Endian.big, bool sign = false}) {
if (byteOrder == Endian.little) {
bytes = List<int>.from(bytes.reversed.toList());
}
BigInt result = BigInt.zero;
for (int i = 0; i < bytes.length; i++) {
/// Add each byte to the result, considering its position and byte order.
result += BigInt.from(bytes[bytes.length - i - 1]) << (8 * i);
}
if (result == BigInt.zero) return BigInt.zero;
if (sign && (bytes[0] & 0x80) != 0) {
final bitLength = bitlengthInBytes(result) * 8;
return result.toSigned(bitLength);
}
return result;
}