NumberRadixUtil { static public String normalizeLeadingHexZeroes(String v, int length) throws ConversionException { if (v == null || v.length() == 0) { throw new ConversionException("Empty or null value detected; unable to convert"); } else if (v.length() == length) { return v; } else if (v.length() < length) { int prepend = (length - v.length()); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < prepend; i++) { sb.append('0'); } sb.append(v); return sb.toString(); } else { int remove = (v.length() - length); for (int i = 0; i < remove; i++) { if (v.charAt(i) != '0') { throw new ConversionOverflowException("Overflow of value detected; unable to trim value [" + v + "] to length " + length); } } return v.substring(remove); } }  static NumberRadixResult parseNumberRadix(String number); static String normalizeLeadingHexZeroes(String v, int length);  }
@Test(expected=ConversionException.class) public void normalizeLeadingHexZeroesNullValue() throws Exception { NumberRadixUtil.normalizeLeadingHexZeroes(null, 2); }
@Test(expected=ConversionException.class) public void normalizeLeadingHexZeroesEmptyValue() throws Exception { NumberRadixUtil.normalizeLeadingHexZeroes("", 2); }
@Test public void normalizeLeadingHexZeroes() throws Exception { Assert.assertEquals("00", NumberRadixUtil.normalizeLeadingHexZeroes("0", 2)); Assert.assertEquals("00", NumberRadixUtil.normalizeLeadingHexZeroes("00", 2)); Assert.assertEquals("0F", NumberRadixUtil.normalizeLeadingHexZeroes("F", 2)); Assert.assertEquals("FF", NumberRadixUtil.normalizeLeadingHexZeroes("FF", 2)); Assert.assertEquals("0000", NumberRadixUtil.normalizeLeadingHexZeroes("0", 4)); Assert.assertEquals("0000", NumberRadixUtil.normalizeLeadingHexZeroes("00", 4)); Assert.assertEquals("000F", NumberRadixUtil.normalizeLeadingHexZeroes("F", 4)); Assert.assertEquals("00FF", NumberRadixUtil.normalizeLeadingHexZeroes("FF", 4)); Assert.assertEquals("0FFF", NumberRadixUtil.normalizeLeadingHexZeroes("FFF", 4)); Assert.assertEquals("FFFF", NumberRadixUtil.normalizeLeadingHexZeroes("FFFF", 4)); Assert.assertEquals("00000000", NumberRadixUtil.normalizeLeadingHexZeroes("0", 8)); Assert.assertEquals("00000000", NumberRadixUtil.normalizeLeadingHexZeroes("00", 8)); Assert.assertEquals("0000000F", NumberRadixUtil.normalizeLeadingHexZeroes("F", 8)); Assert.assertEquals("000000FF", NumberRadixUtil.normalizeLeadingHexZeroes("FF", 8)); Assert.assertEquals("00000FFF", NumberRadixUtil.normalizeLeadingHexZeroes("FFF", 8)); Assert.assertEquals("0000FFFF", NumberRadixUtil.normalizeLeadingHexZeroes("FFFF", 8)); Assert.assertEquals("FFFFFFFF", NumberRadixUtil.normalizeLeadingHexZeroes("FFFFFFFF", 8)); Assert.assertEquals("00", NumberRadixUtil.normalizeLeadingHexZeroes("000", 2)); Assert.assertEquals("00", NumberRadixUtil.normalizeLeadingHexZeroes("0000", 2)); Assert.assertEquals("0F", NumberRadixUtil.normalizeLeadingHexZeroes("00F", 2)); Assert.assertEquals("FF", NumberRadixUtil.normalizeLeadingHexZeroes("000000000FF", 2)); }
@Test(expected=ConversionException.class) public void normalizeLeadingHexZeroesThrowsOverflow() throws Exception { NumberRadixUtil.normalizeLeadingHexZeroes("100", 2); }