Skip to content

Instantly share code, notes, and snippets.

@paullewallencom
Created July 26, 2018 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paullewallencom/a7d28a0ded625ca029f49e52cc3b7e7d to your computer and use it in GitHub Desktop.
Save paullewallencom/a7d28a0ded625ca029f49e52cc3b7e7d to your computer and use it in GitHub Desktop.
Reads Binary File and Writes Hex
public class HexDump
{
private HexDump() { }
public static void main( String[] args )
{
int bytesPerLine = 16;
if ( args.length == 1 )
{
bytesPerLine = Integer.parseInt( args[ 0 ] );
}
int i;
for ( i = 0; !BinaryStdIn.isEmpty(); i++ )
{
if ( bytesPerLine == 0 )
{
BinaryStdIn.readChar();
continue;
}
if ( i == 0 ) StdOut.printf( "" );
else if ( i % bytesPerLine == 0 ) StdOut.printf( "\n", i );
else StdOut.print( " " );
char c = BinaryStdIn.readChar();
StdOut.printf( "%02x", c & 0xff );
}
if ( bytesPerLine != 0 ) StdOut.println();
StdOut.println( ( i*8 ) + " bits" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment