bytesIncrementBigEndian function

void bytesIncrementBigEndian(
  1. Uint8List bytes,
  2. int n
)

Interprets the bytes a big endian integer and increments them by int.

Example

final bytes = [0,2,255];
bytesIncrementBigEndian(bytes, 5);
// bytes become [0,3,4]

Implementation

void bytesIncrementBigEndian(Uint8List bytes, int n) {
  if (n < 0) {
    throw ArgumentError.value(n, 'n');
  }
  // For each byte from the beginning
  for (var i = bytes.length - 1; n != 0 && i >= 0; i--) {
    final newByte = bytes[i] + n;
    bytes[i] = newByte % 0x100;

    // Carry
    n = newByte ~/ 0x100;
  }
}