1 3 import java.io.*; 4 5 public class HexWriter { 6 7 public static void main( String [] args ) 8 { 9 if (args.length != 1 ) { 10 System.err.println( "USAGE: java HexWriter <FILENAME>" ); 11 System.exit(1); 12 } 13 14 String filename = args[0]; 15 int column_width = 10; 16 int retval = 0; 17 18 System.out.println( " // Java hex dump byte data from file:" + filename ); 19 20 FileInputStream fis = null; 21 BufferedInputStream bis = null; 22 23 try { 24 fis = new FileInputStream( filename ); 25 bis = new BufferedInputStream( fis ); 26 27 int k, q, len; 30 byte[] buffer = new byte[1024]; q=0; 32 33 String newline = System.getProperty( "line.separator"); 34 System.out.print( " byte data[] = {" ); 35 36 for (;;) { 37 len = bis.read( buffer, 0, buffer.length ); 38 39 if ( len < 0 ) break; 41 for (k=0; k<len; ++k, ++q) { 42 if ( q % column_width == 0) { 43 System.out.print( newline + "\t" ); 44 } 45 System.out.print( "(byte)" ); 46 System.out.print( "0x"+Integer.toHexString( buffer[q] & 0xFF ) + ", "); 47 } 48 } 49 System.out.println( newline + " };" ); 50 } 51 catch ( IOException ioe ) { 52 retval = 69; 54 System.err.println( "I/O Exception :" + ioe.getMessage() ); 55 } 56 finally { 57 if (bis != null) { 58 try { bis.close(); } catch (IOException ioe) { ; } 60 } 61 if (fis != null) { 62 try { fis.close(); } catch (IOException ioe) { ; } 64 } 65 } 66 67 System.exit( retval ); 68 } 69 } 70 71 | Popular Tags |