bytesToHex static method

HexString bytesToHex(
  1. ByteArray bytes, [
  2. int offset = 0,
  3. int? length
])

Convert a byte array to its hexadecimal string representation

Implementation

static HexString bytesToHex(ByteArray bytes, [int offset = 0, int? length]) {
  length ??= bytes.length - offset;
  if (offset < 0 || offset > bytes.length) {
    throw ArgumentError('Invalid offset: $offset');
  }
  if (length < 0 || offset + length > bytes.length) {
    throw ArgumentError('Invalid length: $length');
  }
  final hexChars = List<String>.filled(length * 2, '');
  for (int j = 0; j < length; j++) {
    final v = bytes[j + offset] & 0xFF;
    hexChars[j * 2] = ByteUtils.hexChars[v >> 4];
    hexChars[j * 2 + 1] = ByteUtils.hexChars[v & 0x0F];
  }
  return hexChars.join();
}