toHexString method
Converts an integer value to a nicely-formatted hexadecimal equivalent.
For example 255.toHex(8)
returns 0xFF.
Takes a parameter bits
indicating the width of the number. Normally
this value will be 8, 16, 32 or 64, but other integers that are divisible
by 8 are permissible.
Implementation
String toHexString(int bits) {
if ((bits % 8) != 0) return '';
// Need to cast to a BigInt because Dart integers are signed 64-bit values
final bigValue = BigInt.from(this);
final value = bigValue.toUnsigned(bits);
return '0x${value.toRadixString(16).padLeft(bits ~/ 4, '0')}';
}