encodeUint64 static method

Uint8List encodeUint64(
  1. BigInt value
)

Encode an non-negative integer as a big-endian uint64.

Implementation

static Uint8List encodeUint64(BigInt value) {
  if (value.compareTo(BigInt.zero) < 0 || value.compareTo(MAX_UINT64) > 0) {
    throw ArgumentError('Value cannot be represented by a uint64');
  }

  final fixedLengthEncoding = Uint8List(UINT64_LENGTH);
  var encodedValue = value.toUint8List();

  if (encodedValue.isNotEmpty && encodedValue[0] == 0) {
    // encodedValue is actually encoded as a signed 2's complement value,
    // so there may be a leading 0 for some encodings -- ignore it
    encodedValue = encodedValue.sublist(1, encodedValue.length);
  }

  final start = UINT64_LENGTH - encodedValue.length;
  final end = start + encodedValue.length;

  fixedLengthEncoding.setRange(start, end, encodedValue);

  return fixedLengthEncoding;
}