Read Java in as Hex

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2011-01-16T02:43:53Z Indexed on 2011/01/16 2:53 UTC
Read the original article Hit count: 248

Filed under:
|
|

Hi, 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.

© Stack Overflow or respective owner

Related posts about java

Related posts about read