Skip to content

Instantly share code, notes, and snippets.

@kofemann
Last active December 12, 2018 17:34
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 kofemann/d6746595ba88745e9b7bf0a1ab1c066d to your computer and use it in GitHub Desktop.
Save kofemann/d6746595ba88745e9b7bf0a1ab1c066d to your computer and use it in GitHub Desktop.
dump data in byte buffer
import java.nio.ByteBuffer;
public class Hexdump {
public static void dumphex(long offset, ByteBuffer buffer) {
byte[] chunk = new byte[16];
while(buffer.hasRemaining()) {
int len = Math.min(chunk.length, buffer.remaining());
buffer.get(chunk, 0, len);
System.out.printf("%05x ", offset);
int i;
for(i = 0; i < len; i++) {
if(i%8 == 0) {
// separator between 8 bytes
System.out.print(" "); // two spaces
}
System.out.printf("%02x ", chunk[i]);
}
if (i < 8) {
// add missing separator
System.out.print(" "); // two spaces
}
// fill missing butes for short arrays
for (; i < chunk.length; i++) {
System.out.print(" "); // three spaces
}
System.out.print(" "); // two spaces
for(i = 0; i < len; i++) {
int c = chunk[i];
if (Character.isDefined(c) && !Character.isWhitespace(c)) {
System.out.print(Character.toString((char)chunk[i]));
} else {
System.out.print('.');
}
}
System.out.println();
offset += len;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment