Creating a binary dump of a file
Hi all,
I'm trying to get back into Java again after a decent period away. I'm currently working on creating a very basic virus searching program. For this to work I need to create a class that is able to take an input stream from a given file and convert this to its binary representation. I am then going to shift it to Hex to make it more readable. This can then be used to scan for any virus signatures I have in my database. I've been experimenting with the different input streams Java provides but am having difficulties figuring out which one would be best.
If anyone can provide me with a pointer in the right direction for which inbuilt Java input streams I should be using or if there is any example code for this.
Thanks,
Neil.
Re: Creating a binary dump of a file
Quote:
take an input stream from a given file and convert this to its binary representation
You can read any file as bytes into a byte array. No need to convert it. It IS binary.
The Integer.toHexString() could be used to convert the bytes to a String hex representation. The problem with the method is that it drops leading 0s.
Or write your own simple converter using a String array with letters 0-F and index it with the nibbles of the byte.
Re: Creating a binary dump of a file
Most streams should work perfectly fine. For this kind of project, a FileReader wrapped in a BufferedReader should work quite nicely.
You can read in the file one byte at a time and then display the hex using the method Norm described (the underlying storage of the numbers is all the same, it's just how you display it).
Re: Creating a binary dump of a file
You might not want to use a Reader. Readers are for character input. You want byte input.
The FileInputStream would read bytes. From the API doc:
Quote:
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
Re: Creating a binary dump of a file
Hey,
This gives me a lot of good starting points, will give them all a try and update on the progress.
The help is really appriciated.
Neil.