How to search in a file efficiently (=fast !) for a certain hex value ?

R

Renli

How can I efficiently (!) and fast search a file for a given hex value (e.g. x'6A')
resp. how can I count the number of occurencies in a given hex value ?

Ulf

The following info should help.
You can make a FileInputStream like this:
FileInputStream fis = new FileInputStream("filename.ext");

You can read in a byte from the file like this:
int value = fis.read(); // if value == -1, it's the end of the file.

You can translate an integer into a hex string like this:
String hexstr = Integer.toHexString(value);

You can translate a hex string into an integer like this:
int value = Integer.valueOf(hexstring, 16);
(or)
int value = Integer.parseInt(hexstring, 16);

-----

There, now you know how to read a byte in from a file; how to detect
EOF, and how to convert both the hex code you're looking for and/or
the int you read into the same format for an equality test. Remember
that if you convert both into a string use .equals() and not == to
determine if they are the same.

Good luck and let us know how it works out.

-
 
R

Renli

Oh heh - the OP crossposted this and only allowed follow ups to cljp.
Sorry about any multiple post guys. I didn't notice it till I decided
to respond again, thinking it didn't go through. I don't read your
newsgroup so easy on the flames ;)

-
 
L

Lew

Renli said:
You can translate a hex string into an integer like this:
int value = Integer.valueOf(hexstring, 16);
(or)
int value = Integer.parseInt(hexstring, 16);

Note that Integer.valueOf() returns an Integer, whereas Integer.parseInt()
returns an int.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top