Hexdump With Offsets And Ascii Columns
Render raw bytes as offset, hex, and ASCII columns in fixed-width rows, the standard shape for inspecting wire protocols and files.
Code
import java.nio.charset.StandardCharsets;
public class HexDump {
// fixed-width columns: four-digit offset, hex pairs, printable ASCII echo
static void hexDump(byte[] bytes) {
for (int offset = 0; offset < bytes.length; offset += 16) {
StringBuilder hex = new StringBuilder();
StringBuilder ascii = new StringBuilder();
for (int i = offset; i < Math.min(offset + 16, bytes.length); i++) {
hex.append(String.format("%02x ", bytes[i] & 0xff)); // bytes are handled as unsigned
ascii.append(bytes[i] >= 0x20 && bytes[i] < 0x7f ? (char) bytes[i] : '.');
}
System.out.printf("%04x %-48s %s%n", offset, hex.toString(), ascii);
}
}
public static void main(String[] args) {
hexDump("GET /health HTTP/1.1".getBytes(StandardCharsets.US_ASCII));
}
}Inside the snippet
Hexdump With Offsets And Ascii Columns comes down to the class HexDump and the functions hexDump and main.
Repetition is confined to for (int offset = 0; offset < bytes.length; offset += 16) — the rest of the file is straight-line.
Using it
The entry-point signature is static void hexDump(byte[] bytes)
main calls 1 helper in sequence: hexDump.
Interactive Tools for This Topic
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.
FAQ
- How do you run Hexdump With Offsets And Ascii Columns?
- Execute it straight away — the program needs nothing beyond the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.