Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - read bytes at given offset

Tags:

java

offset

I'm having a little trouble in understanding how I can read and return the value of a certain offset position in a file.

For example, I know from my hex editor that the offset is D768, and the value is 32bit. So how can read this value and display it in a label.

Any help at all will be greatly appreciated.

like image 997
Bagshot Avatar asked Sep 22 '26 11:09

Bagshot


2 Answers

I think that java.io.RandomAccessFile is your new friend :-)

Beware of the following code, it has not been tested.

RandomAccessFile raf = new RandomAccessFile("foo.bin", "r");
raf.seek(0xd768);
int value = raf.read();
like image 62
Søren Mors Avatar answered Sep 24 '26 00:09

Søren Mors


Use the DataInputStream class. Open the file, and position it using skipBytes(offset), and then call readInt(). This will give you a 32 bit starting at the offset you used.

(Note that this assumes that the integer is represented in the file in with the most significant byte first.)

like image 36
Stephen C Avatar answered Sep 24 '26 00:09

Stephen C