Created
November 4, 2014 10:25
-
-
Save jostly/1fc37cb8cc99f53f91e4 to your computer and use it in GitHub Desktop.
Hexdump for Java byte array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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