James
James

Reputation: 11

Read Java in as Hex

I have tried to solve this but I keep coming up with stuff that is no help I'm sure this is easy (when you know how of course ;) )

What I would like to do is read in a file using a byte stream like below:

while((read = in.read()) != -1){

       //code removed to save space

       Integer.toHexString(read);
System.out.println(read);

}

When it prints out the Hex to the screen it will print out numbers fine e.g 31 13 12 0

but when it comes to a hex code that should be 01 31 it will print 0 131. I want to read it in to a variable like you would see in a hex editor i.e 00 11 21 31 no single numbers as i need to scan the whole file and look for patterns which I know how to do I'm just stuck on this :/

so in short i need a variabe to contain the two hex characters i.e int temp = 01 not int temp = 0 , I hope this all makes sense, I'm a little confused as it's 3am!

If anyone knows how to do this I would be most greatful, p.s thanks for the help in advance this site has saved me loads of research and have learnt a lot!

Many thanks.

Upvotes: 1

Views: 16123

Answers (4)

Ankit Singla
Ankit Singla

Reputation: 198

import org.apache.commons.io.IOUtils;
import org.apache.commons.codec.binary.Hex;

InputStream is = new FileInputStream(new File("c:/file.txt"));
String hexString = Hex.encodeHexString(IOUtils.toByteArray(is));

In java 7 you can read byte array directly from file as below :

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path)     

Upvotes: 2

James
James

Reputation: 433

Hi everyone one posted, thanks for the reply but the way I eneded up doing it was:

                        hexIn = in.read();
                        s = Integer.toHexString(hexIn);
                        if(s.length() < 2){
                            s = "0" + Integer.toHexString(hexIn);
                        }

Just thought I would post they way I did it for anyone else in future, thank you soo much for your help though!

Upvotes: 1

Tony
Tony

Reputation: 1244

This method :

public static void printHexStream(final InputStream inputStream, final int numberOfColumns) throws IOException{
    long streamPtr=0;
    while (inputStream.available() > 0) { 
        final long col = streamPtr++ % numberOfColumns;
        System.out.printf("%02x ",inputStream.read());
        if (col == (numberOfColumns-1)) {
            System.out.printf("\n");
        }
    }
}

will output something like this :

40 32 38 00 5f 57 69 64 65 43 
68 61 72 54 6f 4d 75 6c 74 69 
42 79 74 65 40 33 32 00 5f 5f 
69 6d 70 5f 5f 44 65 6c 65 74 
65 46 69 6c 65 41 40 34 00 5f 
53 65 74 46 69 6c 65 50 6f 69 
6e 74 65 72 40 31 36 00 5f 5f 
69 6d 70 5f 5f 47 65 74 54 65 
6d 70 50 61 74 68 41 40 38 00 

Is it what you are looking for?

Upvotes: 6

Raph Levien
Raph Levien

Reputation: 5218

I think what you're looking for is a formatter. Try:

Formatter formatter = new Formatter();
formatter.format("%02x", your_int);
System.out.println(formatter.toString());

Does that do what you're looking for? Your question wasn't all that clear (and I think maybe you deleted too much code from your snippet).

Upvotes: 2

Related Questions