@Test public void testByteArrayToHexDump() throws IOException { Assert.assertEquals("00: 41 42 AB\n02: 43 C ", byteArrayToHexDump("ABC".getBytes(), 2)); }
public static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine) { assert ab != null; assert of >= 0 && cb >= 0 && of + cb <= ab.length; assert cBytesPerLine > 0; final int cchAddr = countHexDigits(of + cb) + 1 & ~0x01; final int cch = cchAddr + cBytesPerLine * 4 + 3; final char[] ach = new char[cch]; Arrays.fill(ach, ' '); ach[cchAddr] = ':'; ach[cch-1] = '\n'; final int cLines = Math.max((cb + cBytesPerLine - 1) / cBytesPerLine, 1); final StringBuilder sb = new StringBuilder(cLines * cch); final int ofHex = cchAddr + 2; final int ofChar = ofHex + cBytesPerLine * 3; for (int iLine = 0; iLine < cLines; ++iLine) { renderIntToHex(of, ach, 0, cchAddr); for (int iByte = 0; iByte < cBytesPerLine; ++iByte) { if (cb > 0) { int b = ab[of]; renderByteToHex(b, ach, ofHex + iByte * 3); char ch = (char) (b & 0xFF); ach[ofChar + iByte] = Character.isISOControl(ch) ? '.' : ch; } else { ach[ofHex + iByte * 3 ] = ' '; ach[ofHex + iByte * 3 + 1] = ' '; ach[ofChar + iByte ] = ' '; } ++of; --cb; } sb.append(ach, 0, iLine < cLines - 1 ? cch : cch - 1); } return sb.toString(); }
Handy { public static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine) { assert ab != null; assert of >= 0 && cb >= 0 && of + cb <= ab.length; assert cBytesPerLine > 0; final int cchAddr = countHexDigits(of + cb) + 1 & ~0x01; final int cch = cchAddr + cBytesPerLine * 4 + 3; final char[] ach = new char[cch]; Arrays.fill(ach, ' '); ach[cchAddr] = ':'; ach[cch-1] = '\n'; final int cLines = Math.max((cb + cBytesPerLine - 1) / cBytesPerLine, 1); final StringBuilder sb = new StringBuilder(cLines * cch); final int ofHex = cchAddr + 2; final int ofChar = ofHex + cBytesPerLine * 3; for (int iLine = 0; iLine < cLines; ++iLine) { renderIntToHex(of, ach, 0, cchAddr); for (int iByte = 0; iByte < cBytesPerLine; ++iByte) { if (cb > 0) { int b = ab[of]; renderByteToHex(b, ach, ofHex + iByte * 3); char ch = (char) (b & 0xFF); ach[ofChar + iByte] = Character.isISOControl(ch) ? '.' : ch; } else { ach[ofHex + iByte * 3 ] = ' '; ach[ofHex + iByte * 3 + 1] = ' '; ach[ofChar + iByte ] = ' '; } ++of; --cb; } sb.append(ach, 0, iLine < cLines - 1 ? cch : cch - 1); } return sb.toString(); } }
Handy { public static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine) { assert ab != null; assert of >= 0 && cb >= 0 && of + cb <= ab.length; assert cBytesPerLine > 0; final int cchAddr = countHexDigits(of + cb) + 1 & ~0x01; final int cch = cchAddr + cBytesPerLine * 4 + 3; final char[] ach = new char[cch]; Arrays.fill(ach, ' '); ach[cchAddr] = ':'; ach[cch-1] = '\n'; final int cLines = Math.max((cb + cBytesPerLine - 1) / cBytesPerLine, 1); final StringBuilder sb = new StringBuilder(cLines * cch); final int ofHex = cchAddr + 2; final int ofChar = ofHex + cBytesPerLine * 3; for (int iLine = 0; iLine < cLines; ++iLine) { renderIntToHex(of, ach, 0, cchAddr); for (int iByte = 0; iByte < cBytesPerLine; ++iByte) { if (cb > 0) { int b = ab[of]; renderByteToHex(b, ach, ofHex + iByte * 3); char ch = (char) (b & 0xFF); ach[ofChar + iByte] = Character.isISOControl(ch) ? '.' : ch; } else { ach[ofHex + iByte * 3 ] = ' '; ach[ofHex + iByte * 3 + 1] = ' '; ach[ofChar + iByte ] = ' '; } ++of; --cb; } sb.append(ach, 0, iLine < cLines - 1 ? cch : cch - 1); } return sb.toString(); }  }
Handy { public static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine) { assert ab != null; assert of >= 0 && cb >= 0 && of + cb <= ab.length; assert cBytesPerLine > 0; final int cchAddr = countHexDigits(of + cb) + 1 & ~0x01; final int cch = cchAddr + cBytesPerLine * 4 + 3; final char[] ach = new char[cch]; Arrays.fill(ach, ' '); ach[cchAddr] = ':'; ach[cch-1] = '\n'; final int cLines = Math.max((cb + cBytesPerLine - 1) / cBytesPerLine, 1); final StringBuilder sb = new StringBuilder(cLines * cch); final int ofHex = cchAddr + 2; final int ofChar = ofHex + cBytesPerLine * 3; for (int iLine = 0; iLine < cLines; ++iLine) { renderIntToHex(of, ach, 0, cchAddr); for (int iByte = 0; iByte < cBytesPerLine; ++iByte) { if (cb > 0) { int b = ab[of]; renderByteToHex(b, ach, ofHex + iByte * 3); char ch = (char) (b & 0xFF); ach[ofChar + iByte] = Character.isISOControl(ch) ? '.' : ch; } else { ach[ofHex + iByte * 3 ] = ' '; ach[ofHex + iByte * 3 + 1] = ' '; ach[ofChar + iByte ] = ' '; } ++of; --cb; } sb.append(ach, 0, iLine < cLines - 1 ? cch : cch - 1); } return sb.toString(); }  static char nibbleToChar(int n); static StringBuilder appendByteAsHex(StringBuilder sb, int n); static String byteToHexString(int n); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab, int of, int cb); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab); static String byteArrayToHexString(byte[] ab, int of, int cb); static String byteArrayToHexString(byte[] ab); static byte[] hexStringToByteArray(String s); static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine); static String byteArrayToHexDump(byte[] ab, int cBytesPerLine); static int countHexDigits(int n); static StringBuilder appendIntAsHex(StringBuilder sb, int n, int cch); static StringBuilder appendIntAsHex(StringBuilder sb, int n); static String intToHexString(int n); static int countHexDigits(long n); static StringBuilder appendLongAsHex(StringBuilder sb, long n, int cch); static StringBuilder appendLongAsHex(StringBuilder sb, long n); static String longToHexString(long n); static int renderByteToHex(int n, char[] ach, int of); static int renderIntToHex(int n, char[] ach, int of, int cch); static int renderLongToHex(long n, char[] ach, int of, int cch); static boolean isDigit(char ch); static int digitValue(char ch); static boolean isHexit(char ch); static int hexitValue(char ch); static boolean isAsciiLetter(char ch); static int countChar(String s, char ch); static String[] parseDelimitedString(String s, char chDelim); static String indentLines(String sText, String sIndent); static boolean isCharEscaped(char ch); static StringBuilder appendChar(StringBuilder sb, char ch); static String quotedChar(char ch); static StringBuilder appendString(StringBuilder sb, String s); static String quotedString(String s); static long readPackedLong(DataInput in); static void writePackedLong(DataOutput out, long n); static int readPackedInt(DataInput in); static int readMagnitude(DataInput in); static int readIndex(DataInput in); static int readUtf8Char(DataInput in); static void writeUtf8Char(DataOutput out, int ch); static String readUtf8String(DataInput in); static void writeUtf8String(DataOutput out, String s); static InputStream toInputStream(File file); static byte[] readFileBytes(File file); static char[] readFileChars(File file); static char[] readFileChars(File file, String sEncoding); static T[] dedupAdds(T[] aoBase, T[] aoAdd); static T[] append(T[] aoBase, T[] aoAdd); static T[] appendHead(T[] aoBase, T oAdd); static ArrayList<T> startList(T[] ao, int c); static ArrayList<T> appendList(ArrayList<T> list, T[] ao, int of, int c); static boolean anyMatches(T[] ao, T o); static int hashCode(final Object o); static boolean equals(final Object o1, final Object o2); static int compareObjects(final Comparable o1, final Comparable o2); static boolean equalArraysNullOk(final Object[] ao1, final Object[] ao2); static int compareArrays(final Comparable[] ao1, final Comparable[] ao2); static boolean checkElementsNonNull(Object[] ao); }
Handy { public static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine) { assert ab != null; assert of >= 0 && cb >= 0 && of + cb <= ab.length; assert cBytesPerLine > 0; final int cchAddr = countHexDigits(of + cb) + 1 & ~0x01; final int cch = cchAddr + cBytesPerLine * 4 + 3; final char[] ach = new char[cch]; Arrays.fill(ach, ' '); ach[cchAddr] = ':'; ach[cch-1] = '\n'; final int cLines = Math.max((cb + cBytesPerLine - 1) / cBytesPerLine, 1); final StringBuilder sb = new StringBuilder(cLines * cch); final int ofHex = cchAddr + 2; final int ofChar = ofHex + cBytesPerLine * 3; for (int iLine = 0; iLine < cLines; ++iLine) { renderIntToHex(of, ach, 0, cchAddr); for (int iByte = 0; iByte < cBytesPerLine; ++iByte) { if (cb > 0) { int b = ab[of]; renderByteToHex(b, ach, ofHex + iByte * 3); char ch = (char) (b & 0xFF); ach[ofChar + iByte] = Character.isISOControl(ch) ? '.' : ch; } else { ach[ofHex + iByte * 3 ] = ' '; ach[ofHex + iByte * 3 + 1] = ' '; ach[ofChar + iByte ] = ' '; } ++of; --cb; } sb.append(ach, 0, iLine < cLines - 1 ? cch : cch - 1); } return sb.toString(); }  static char nibbleToChar(int n); static StringBuilder appendByteAsHex(StringBuilder sb, int n); static String byteToHexString(int n); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab, int of, int cb); static StringBuilder appendByteArrayAsHex(StringBuilder sb, byte[] ab); static String byteArrayToHexString(byte[] ab, int of, int cb); static String byteArrayToHexString(byte[] ab); static byte[] hexStringToByteArray(String s); static String byteArrayToHexDump(byte[] ab, int of, int cb, int cBytesPerLine); static String byteArrayToHexDump(byte[] ab, int cBytesPerLine); static int countHexDigits(int n); static StringBuilder appendIntAsHex(StringBuilder sb, int n, int cch); static StringBuilder appendIntAsHex(StringBuilder sb, int n); static String intToHexString(int n); static int countHexDigits(long n); static StringBuilder appendLongAsHex(StringBuilder sb, long n, int cch); static StringBuilder appendLongAsHex(StringBuilder sb, long n); static String longToHexString(long n); static int renderByteToHex(int n, char[] ach, int of); static int renderIntToHex(int n, char[] ach, int of, int cch); static int renderLongToHex(long n, char[] ach, int of, int cch); static boolean isDigit(char ch); static int digitValue(char ch); static boolean isHexit(char ch); static int hexitValue(char ch); static boolean isAsciiLetter(char ch); static int countChar(String s, char ch); static String[] parseDelimitedString(String s, char chDelim); static String indentLines(String sText, String sIndent); static boolean isCharEscaped(char ch); static StringBuilder appendChar(StringBuilder sb, char ch); static String quotedChar(char ch); static StringBuilder appendString(StringBuilder sb, String s); static String quotedString(String s); static long readPackedLong(DataInput in); static void writePackedLong(DataOutput out, long n); static int readPackedInt(DataInput in); static int readMagnitude(DataInput in); static int readIndex(DataInput in); static int readUtf8Char(DataInput in); static void writeUtf8Char(DataOutput out, int ch); static String readUtf8String(DataInput in); static void writeUtf8String(DataOutput out, String s); static InputStream toInputStream(File file); static byte[] readFileBytes(File file); static char[] readFileChars(File file); static char[] readFileChars(File file, String sEncoding); static T[] dedupAdds(T[] aoBase, T[] aoAdd); static T[] append(T[] aoBase, T[] aoAdd); static T[] appendHead(T[] aoBase, T oAdd); static ArrayList<T> startList(T[] ao, int c); static ArrayList<T> appendList(ArrayList<T> list, T[] ao, int of, int c); static boolean anyMatches(T[] ao, T o); static int hashCode(final Object o); static boolean equals(final Object o1, final Object o2); static int compareObjects(final Comparable o1, final Comparable o2); static boolean equalArraysNullOk(final Object[] ao1, final Object[] ao2); static int compareArrays(final Comparable[] ao1, final Comparable[] ao2); static boolean checkElementsNonNull(Object[] ao); static byte[] EMPTY_BYTE_ARRAY; static char[] EMPTY_CHAR_ARRAY; static String[] NO_ARGS; }