Last active
December 12, 2018 17:34
-
-
Save kofemann/d6746595ba88745e9b7bf0a1ab1c066d to your computer and use it in GitHub Desktop.
dump data in byte buffer
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
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