intToHex method

String intToHex (int val, [ int pad = 0 ])

Integer to hex string with leading 0x, lowercase. The optional pad value pads the string out to the number of bytes specified, i.e if 8 is specified the string 0x1 becomes 0x0000000000000001 default is 0, no padding.The pad value must be even and positive.

Implementation

static String intToHex(int val, [int pad = 0]) {
  String ret = val.toRadixString(16);
  if (pad != 0) {
    if (pad.isNegative || pad.isOdd) {
      throw FormatException(
          "MoacUtilities:: intToHex - invalid pad value, $pad");
    }
    if (ret.length.isOdd) {
      ret = '0' + ret;
    }
    final int bytes = (ret.length / 2).round();
    if (bytes != pad) {
      final int zeroNum = (pad - bytes);
      for (int i = 0; i < zeroNum; i++) {
        ret = '00' + ret;
      }
    }
  }
  return '0x' + ret;
}