// onjava/Hex.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. package onjava; import java.io.*; import java.nio.file.*; public class Hex { public static String format(byte[] data) { StringBuilder result = new StringBuilder(); int n = 0; for(byte b : data) { if(n % 16 == 0) result.append(String.format("%05X: ", n)); result.append(String.format("%02X ", b)); n++; if(n % 16 == 0) result.append("\n"); } result.append("\n"); return result.toString(); } public static void main(String[] args) throws Exception { if(args.length == 0) // Test by displaying this class file: System.out.println(format( Files.readAllBytes(Paths.get("Hex.class")))); else System.out.println(format( Files.readAllBytes(Paths.get(args[0])))); } } /* Output: (First 6 Lines) 00000: CA FE BA BE 00 00 00 34 00 53 0A 00 05 00 22 07 00010: 00 23 0A 00 02 00 22 08 00 24 07 00 25 0A 00 26 00020: 00 27 0A 00 28 00 29 0A 00 02 00 2A 08 00 2B 0A 00030: 00 2C 00 2D 08 00 2E 0A 00 02 00 2F 09 00 30 00 00040: 31 08 00 32 0A 00 33 00 34 0A 00 15 00 35 0A 00 00050: 36 00 37 07 00 38 0A 00 12 00 39 0A 00 33 00 3A ... */