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.

JavaCli Tooling

Code

snippet
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

FAQ

How do you run Hexdump With Offsets And Ascii Columns?
Execute it straight away — the program needs nothing beyond the runtime.