HexDump.java


Below is the syntax highlighted version of HexDump.java from § Standard Libraries.


/*************************************************************************
 *  Compilation:  javac HexDump.java
 *  Execution:    java HexDump < file
 *  Dependencies: BinaryStdIn.java
 *  
 *  Reads in a binary file and writes out the bytes in hex, 16 per line.
 *
 *  % java HexDump < input.txt
 *
 *  % hexdump < input.txt
 *  % od -t x1 < iput.txt
 *
 *************************************************************************/

public class HexDump {

    public static void main(String[] args) {
        int BYTES_PER_LINE = 16;
        if (args.length == 1) {
            BYTES_PER_LINE = Integer.parseInt(args[0]);
        }

        int i;
        for (i = 0; !BinaryStdIn.isEmpty(); i++) {
            if (BYTES_PER_LINE == 0) continue;
            if (i == 0) StdOut.printf("");
            else if (i % BYTES_PER_LINE == 0) StdOut.printf("\n", i);
            else StdOut.print(" ");
            char c = BinaryStdIn.readChar();
            StdOut.printf("%02x", c & 0xff);
        }
        StdOut.println();
        // StdOut.println(i + " bytes");
        StdOut.println((i*8) + " bits");
    }
}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Sep 30 07:31:35 EDT 2009.