Skip to content

Instantly share code, notes, and snippets.

@jostly
Created November 4, 2014 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jostly/1fc37cb8cc99f53f91e4 to your computer and use it in GitHub Desktop.
Save jostly/1fc37cb8cc99f53f91e4 to your computer and use it in GitHub Desktop.
Hexdump for Java byte array
StringBuilder output = new StringBuilder();
int col = 0;
int pos = 0;
String[] chars = new String[16];
for (byte b : array) {
if (col == 0) {
output.append("\n" + String.format("%04x", pos) + ": ");
}
output.append(String.format("%02x ", b));
if (b > 31 && b < 128) {
chars[col] = String.format("%c", b);
}
else {
chars[col] = ".";
}
pos ++;
col ++;
if (col == 16) {
col = 0;
output.append(" ");
for (String c : chars) {
output.append(c);
}
}
}
System.err.println(output.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment