hexFixLength function

String hexFixLength(
  1. String value, {
  2. int bitLength = -1,
  3. bool withPadding = false,
  4. bool include0x = true,
})

Implementation

String hexFixLength(
  String value, {
  int bitLength = -1,
  bool withPadding = false,
  bool include0x = true,
}) {
  final strLength = (bitLength / 4).ceil();
  final hexLength = strLength + 2;
  String beforeAdd;
  if (bitLength == -1 ||
      value.length == hexLength ||
      (!withPadding && value.length < hexLength)) {
    beforeAdd = hexStripPrefix(value);
  } else {
    if (value.length > hexLength) {
      final stripped = hexStripPrefix(value);
      beforeAdd = stripped.substring(stripped.length - 1 * strLength);
    } else {
      final stripped2 = "${'0' * strLength}${hexStripPrefix(value)}";
      beforeAdd = stripped2.substring(stripped2.length - 1 * strLength);
    }
  }
  if (include0x) {
    return hexAddPrefix(beforeAdd);
  }
  return beforeAdd;
}