/** * Hexdump.java * * Dumps a binary file to a ascii, in hexadecimal. * Copyright (c) 1999-2003 W.Finlay McWalter and the Free Software Foundation * Licence: version 2 of the GNU General Public Licence * */ import java.io.*; public class Hexdump { private static final int BYTES_PER_LINE = 16; private static final int PADDING = 4; /** * returns a char corresponding to the hex digit of the * supplied integer. */ private static char charFromHexDigit(int digit){ if((digit >= 0) && (digit <= 9)) return (char)(digit + '0'); else return (char)(digit - 10 + 'a'); } /** * hexdumps the contents of a given open input file to stdout */ private static void dumpFile(FileInputStream in){ try { byte[] inBuf = new byte[BYTES_PER_LINE]; int count; int runningCount=0; StringBuffer line = new StringBuffer(); while((count = in.read(inBuf)) >0){ // print running offset for(int d=0; d < 8; d++){ int digitValue = (int)(runningCount >> (4*(7-d)))%16; line.append (charFromHexDigit((digitValue>=0)?digitValue :(16+digitValue))); } runningCount += count; line.append(" "); // print hexbytes for(int x=0; x=0)?inBuf[x]:(256+inBuf[x]); line.append(charFromHexDigit(i/16)); line.append(charFromHexDigit(i%16)); line.append(' '); } // print padding between hexbytes and ascii for(int x=0; x < PADDING + ((BYTES_PER_LINE-count)*3); x++){ line.append(' '); } // print ascii for(int x=0; x= ' ') && (v < (char)0x7f)){ line.append(v); } else { line.append('.'); } } line.append('\n'); } // while System.out.println(line); } catch (IOException e){ System.out.println("error reading file"); } } public static final void main (String [] args){ if(args.length == 1){ try { FileInputStream inStream = new FileInputStream(args[0]); dumpFile(inStream); inStream.close(); } catch (FileNotFoundException e){ System.out.println("File \"" + args[0] + "\" not found"); } catch (IOException e){ System.out.println("error closing file"); } } else { System.out.println("usage:\n java Hexdump \n"); } } }