decimalToHex static method

String decimalToHex(
  1. int number, [
  2. int minDigits = 1
])

Implementation

static String decimalToHex(int number, [int minDigits = 1]) {
  final _HEX_DIGITS = '0123456789abcdef';

  var result = <String>[];

  var dividend = number >> 4;
  var remain = number % 16;
  result.add(_HEX_DIGITS[remain]);
  while (dividend != 0) {
    remain = dividend % 16;
    dividend >>= 4;
    result.add(_HEX_DIGITS[remain]);
  }

  var invertResult = StringBuffer();
  var paddings = minDigits - result.length;
  while (paddings-- > 0) {
    invertResult.write('0');
  }
  for (var i = result.length - 1; i >= 0; i--) {
    invertResult.write(result[i]);
  }

  return invertResult.toString();
}