HexDumper

M. Gallant 11/10/2000

The ability to create objects from classes in the Java API can be used to extend the capability of Windows scripts. Since the MS Java API is resident on any PC with IE4+ installed, developers can leverage the capability contained therein.
The following script instantiates a Java object which contains a method:
HexDumper.toHexString(String filepath)

which saves a hexdump of files specified as arguments (or drag/dropped). To use the Java class, compile the Java source code, and place the resultant HexDumper.class class file in the classpath (for example: C:\Winnt\Java\Classes)

hexdumper.vbs

'******************************************************************** ' File: hexdumper.vbs (WSH for VBscript) ' Author: (c) M. Gallant 11/10/2000 ' ' Hexdumper utility. ' Outputs text file "_hex_filename.txt" to current script directory. ' Uses Java class "HexDumper" which must be in classpath. '******************************************************************** Option Explicit Dim WshShell, fso, scriptpath, oJfilehexer, oHexfile, outfile, oFiles DIM hexdata, sourcefile, fileargs, i, j set WshShell = WScript.CreateObject("WScript.Shell") set fso = WScript.CreateObject("Scripting.FileSystemObject") scriptpath = fso.GetParentFolderName(WSCript.ScriptFullName) & "\" set oJfilehexer = GetObject("java:HexDumper") 'Instantiate Java object via moniker. Set fileargs = WScript.Arguments If fileargs.Count<1 Then WScript.Echo "Drag some files onto the icon" WScript.Quit End If For i = 0 to fileargs.Count -1 'Get all files passed (or drag/dropped). sourcefile = fileargs(i) If fso.FileExists(sourcefile) Then ' ensure a file and not a folder hexdata = oJfilehexer.toHexString(sourcefile) ' string with hex data outfile = scriptpath & "_hex_" & fso.GetFile(sourcefile).Name & ".txt" Set oHexFile = fso.OpenTextFile(outfile, 2, True) oHexFile.write hexdata oHexFile.close Set oHexFile = Nothing WshShell.Run "notepad.exe " & outfile, 1, True Else WScript.Echo sourcefile & " is not a file." End If Next

HexDumper.java (Java class)

/* File hexdumper for similar to win32 debug.exe utility. M. Gallant 11/10/2000 */ import java.io.*; import java.util.Date; import java.text.DateFormat; public final class HexDumper { static final String nl = java.lang.System.getProperty("line.separator"); public final String toHexString(String filepath){ StringBuffer strbuf = new StringBuffer(1024) ; int filebytes=0; try { String today = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(new Date()); long flen = (new File(filepath)).length() ; strbuf.append("-- " + filepath + " [" +flen + " bytes] " + today + " --"+nl+nl) ; FileInputStream file = new FileInputStream (filepath); DataInputStream in = new DataInputStream (file); byte[] b = new byte[in.available ()]; in.readFully (b); in.close (); filebytes=b.length ; if(filebytes==0) return("File has no bytes") ; int achar=0; int counter = 0; String pad = "0000 " ; StringBuffer buffdisp = new StringBuffer() ; // default 16 byte buffer for displayable characters. strbuf.append(pad) ; for(int i=0; i<filebytes; i++) { achar = b[i] ; if(achar<0) achar+=256; if(achar>31 && achar<127) buffdisp.append((char) achar); else buffdisp.append(".") ; //placeholder for nondisplayable characters. if (achar>=16){ strbuf.append(" " + Integer.toHexString(achar).toUpperCase()) ; } else { strbuf.append(" 0" + Integer.toHexString(achar).toUpperCase()) ; } if((++counter %16) ==0){ // start new line every 16 bytes and if not at end. strbuf.append(" " + buffdisp.toString() + nl) ; buffdisp=new StringBuffer() ; // reset if(counter%128 ==0) strbuf.append(nl) ; // group in 128 byte groups if(counter==16) pad="00"; else if(counter==256) pad="0"; else if(counter==4096) pad=""; if(counter<filebytes) // if not at end of file yet, start new line strbuf.append(pad+Integer.toHexString(counter)+" ") ; // byte counter at start of line. } // end if ... } // end for(int i for(short k=1; k<=(16-(counter%16)); k++) // if only partial line printed.. strbuf.append(" "); // shift final buffdisp to align with other lines. strbuf.append(" " + buffdisp.toString() + nl) ; //print displayable characters and finish line. strbuf.append(nl + "---------------------- Read " + counter + " bytes of data ----------------------------" + nl) ; return(strbuf.toString()) ; } // end try catch (Exception e) { e.printStackTrace() ; // send details to stdio. return("Problem with reading file \n " + e); } } public static void main(String args[]) { // test class HexDumper hstr = new HexDumper() ; String datastring = hstr.toHexString(args[0]); System.out.println(datastring) ; } }