Skip to content

Instantly share code, notes, and snippets.

@I3rixon
Created July 29, 2013 00:46
Show Gist options
  • Save I3rixon/6101489 to your computer and use it in GitHub Desktop.
Save I3rixon/6101489 to your computer and use it in GitHub Desktop.
pretty output of hexdump like in Java
public static void prettyOut(byte[] msg) {
for (int j = 1; j < msg.length+1; j++) {
if (j % 8 == 1 || j == 0) {
if( j != 0){
System.out.println();
}
System.out.format("0%d\t|\t", j / 8);
}
System.out.format("%02X", msg[j-1]);
if (j % 4 == 0) {
System.out.print(" ");
}
}
System.out.println();
}
@RonanKER
Copy link

RonanKER commented Aug 9, 2016

Thanks for your code, I integrated it in my debug tools like that :

  /**
   * Formats Hex view of a byte array<br/>
   * Example : DebugUtil.prettyHexView(testString.getBytes())<br/>
   * testString = "Test1234567890"<br/>
   * output : <pre>00   |   54657374 31323334 
01  |   35363738 3930</pre>
   * @param ba the byte array to format
   * @return String representation "pretty Hex View" of input parameter
   */
  public static String prettyHexView(byte[] ba) {
    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb);
    for (int j = 1; j < ba.length+1; j++) {
      if (j % 8 == 1 || j == 0) {
        if( j != 0){
          sb.append("\n");
        }
        formatter.format("0%d\t|\t", j / 8);
      }
      formatter.format("%02X", ba[j-1]);
      if (j % 4 == 0) {
          sb.append(" ");
      }
    }
    sb.append("\n");
    return sb.toString();
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment