intToUint8List function

Uint8List intToUint8List(
  1. int? value, {
  2. int bitLength = -1,
  3. bool isLittleEndian = true,
  4. bool isNegative = false,
})

Little endian

Implementation

Uint8List intToUint8List(int? value,
    {int bitLength = -1, bool isLittleEndian = true, bool isNegative = false}) {
  if (value == null) {
    return bitLength == -1 ? Uint8List(1) : Uint8List((bitLength + 7) >> 3);
  }

  BigInt valueBn = BigInt.from(value);

  final int byteLength =
      bitLength == -1 ? (valueBn.bitLength + 7) >> 3 : ((bitLength + 7) >> 3);

  final Uint8List output = Uint8List(byteLength);
  if (isNegative) {
    valueBn = valueBn.toUnsigned(byteLength * 8);
  }

  final List<int> bytes = bigIntToBytes(valueBn, byteLength, isLittleEndian);
  output.setAll(0, bytes);

  return output;
}