bytesToHex function
Converts the bytes given as a list of integers into a hexadecimal
representation.
If any of the bytes is outside of the range 0, 256, the method will throw.
The outcome of this function will prefix a 0 if it would otherwise not be
of even length. If include0x is set, it will prefix "0x" to the hexadecimal
representation. If forcePadLength is set, the hexadecimal representation
will be expanded with zeroes until the desired length is reached. The "0x"
prefix does not count for the length.
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;
}