bytesToHex function

String bytesToHex(
  1. List<int> bytes, {
  2. bool include0x = false,
  3. int? forcePadLength,
  4. bool padToEvenLength = false,
})

Implementation

String bytesToHex(List<int> bytes,
    {bool include0x = false,
      int? forcePadLength,
      bool padToEvenLength = false}) {
  var encoded = hex.encode(bytes);

  if (forcePadLength != null) {
    assert(forcePadLength >= encoded.length);

    final padding = forcePadLength - encoded.length;
    encoded = ('0' * padding) + encoded;
  }

  if (padToEvenLength && encoded.length % 2 != 0) {
    encoded = '0$encoded';
  }

  return (include0x ? '0x' : '') + encoded;
}