### Question:
HexFormat { public static String format(byte[] array, int len) { StringBuilder buf = new StringBuilder(); int lines = (len + 15) / 16; for (int i = 0; i < lines; i++) { int off = i * 16; buf.append(toHex(off, 4)); buf.append(" "); int bytes = Math.min(16, len - off); int j; for (j = 0; j < bytes; j++) { if (j == 8) buf.append(' '); buf.append(toHex(array[off + j] & 0xff, 2)); buf.append(' '); } for (; j < 16; j++) { if (j == 8) buf.append(' '); buf.append(" "); } buf.append(" "); for (j = 0; j < bytes; j++) { if (j == 8) buf.append(' '); int c = array[off + j] & 0xff; if (c >= 32 && c < 126) { buf.append((char) c); } else { buf.append('.'); } } if (i != (lines - 1)) buf.append('\n'); } return buf.toString(); } private  HexFormat(); static String format(byte[] array, int len);  }### Answer:
@Test public void testLongLine() { byte[] array = "hello world".getBytes(); String str = HexFormat.format(array, array.length); assertEquals("0000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello wo rld", str); }
@Test public void testMultipleLines() { byte[] array = "The quick brown fox jumped over the lazy dog.".getBytes(); String str = HexFormat.format(array, array.length); assertEquals( "0000 54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 The quic k brown \n" + "0010 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20 fox jump ed over \n" + "0020 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e the lazy dog.", str); }
@Test public void testEmpty() { byte[] array = new byte[0]; String str = HexFormat.format(array, array.length); assertEquals("", str); }
@Test public void testShortLine() { byte[] array = "hello".getBytes(); String str = HexFormat.format(array, array.length); assertEquals("0000 68 65 6c 6c 6f hello", str); }