encodeBigInt static method

Uint8List encodeBigInt(
  1. BigInt input, {
  2. Endian endian = Endian.be,
  3. int length = 0,
})

Implementation

static Uint8List encodeBigInt(
  BigInt input, {
  Endian endian = Endian.be,
  int length = 0,
}) {
  int byteLength = (input.bitLength + 7) >> 3;
  int reqLength = length > 0 ? length : max(1, byteLength);
  assert(byteLength <= reqLength, 'byte array longer than desired length');
  assert(reqLength > 0, 'Requested array length <= 0');

  var res = Uint8List(reqLength);
  res.fillRange(0, reqLength - byteLength, 0);

  var q = input;
  if (endian == Endian.be) {
    for (int i = 0; i < byteLength; i++) {
      res[reqLength - i - 1] = (q & _byteMask).toInt();
      q = q >> 8;
    }
    return res;
  }

  return Uint8List(0);
}