Created
July 26, 2018 18:41
-
-
Save paullewallencom/a7d28a0ded625ca029f49e52cc3b7e7d to your computer and use it in GitHub Desktop.
Reads Binary File and Writes Hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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