Thread View: borland.public.jbuilder.java.language
2 messages
2 total messages
Started by Magnus
Mon, 16 Oct 2006 17:23
Problem reading numbers from a binary file
Author: Magnus
Date: Mon, 16 Oct 2006 17:23
Date: Mon, 16 Oct 2006 17:23
61 lines
2929 bytes
2929 bytes
Hi,
I have a binary file that was written by a Delphi application and I got
the description of all the records and structures in the file.
I did a search to learn how to read a binary file and found
http://www.heatonresearch.com/articles/22/page2.html. I use the
BinaryFile class to read the file and I have managed to get all strings
out of the file.
My problem is that I can't get a single number right so I made a program
that seek (below) through the whole file but without luck.
The seek program sets the "Endian" and "signed" in all combinations for
all positions through out the binary file. I do find some values but
not at the right position and not the expected values.
I have also tried:
- a "Long" version
- used readWord (2 bytes)
- used java's readFloat()
but no luck.
Is there anyone who can help me get my bits straight?
TIA
---
static void seekFloatValue(File ff, float value) throws
FileNotFoundException {
RandomAccessFile file = new RandomAccessFile(ff, "r");
BinaryFile bin = new BinaryFile(file);
try {
long len = file.length();
System.out.println("seekValueFloat value="+value+" len="+len+"
FileName:"+ff);
float tmp=0;
for (int i = 0; i<=len-4; i++) {
for (int j=0;j<4;j++) {
switch(j) {
case 0:bin.setEndian(bin.BIG_ENDIAN);bin.setSigned(true);
break;
case 1:bin.setEndian(bin.LITTLE_ENDIAN);bin.setSigned(true);
break;
case 2:bin.setEndian(bin.BIG_ENDIAN);bin.setSigned(false);
break;
default:bin.setEndian(bin.LITTLE_ENDIAN);bin.setSigned(false);
}
bin.setPos(i);
tmp = (float) bin.readDWord();
if (tmp == value)
System.out.println("pos" + i + " tmp=" + tmp + " ENDIAN=" +
bin.getEndian() + " signed=" +
bin.getSigned());
}
}
} catch (Exception e) {
System.out.println("**Error: " + e.getMessage());
}
}
Re: Problem reading numbers from a binary file
Author: Magnus
Date: Wed, 18 Oct 2006 13:16
Date: Wed, 18 Oct 2006 13:16
91 lines
3982 bytes
3982 bytes
I posted my problem on a google group (comp.lang.java.programmer) and
got a solution that works:
> Example: Somewhere in the Hex below store: 40.0 Single (Delphi = 4
> bytes)
> Hex From file: 00 00 00 00 20 42 00 00 00
> - I assume to read 00 00 20 42 but can read from any pos ...
> I have tried any type of read and conversion but can't get this one
> right (or any floats) from the file.
public float readFP() throws java.io.IOException {
byte a, b, c, d;
a = _file.readByte();
b = _file.readByte();
c = _file.readByte();
d = _file.readByte();
byte[] ba = { d, c, b, a };
DataInputStream dis = new DataInputStream(new
ByteArrayInputStream(ba));
float x = dis.readFloat();
return x;
}
Magnus wrote:
> Hi,
>
> I have a binary file that was written by a Delphi application and I got
> the description of all the records and structures in the file.
>
> I did a search to learn how to read a binary file and found
> http://www.heatonresearch.com/articles/22/page2.html. I use the
> BinaryFile class to read the file and I have managed to get all strings
> out of the file.
>
> My problem is that I can't get a single number right so I made a program
> that seek (below) through the whole file but without luck.
> The seek program sets the "Endian" and "signed" in all combinations for
> all positions through out the binary file. I do find some values but
> not at the right position and not the expected values.
> I have also tried:
> - a "Long" version
> - used readWord (2 bytes)
> - used java's readFloat()
> but no luck.
>
> Is there anyone who can help me get my bits straight?
>
> TIA
>
> ---
> static void seekFloatValue(File ff, float value) throws
> FileNotFoundException {
> RandomAccessFile file = new RandomAccessFile(ff, "r");
> BinaryFile bin = new BinaryFile(file);
>
> try {
> long len = file.length();
> System.out.println("seekValueFloat value="+value+" len="+len+"
> FileName:"+ff);
> float tmp=0;
> for (int i = 0; i<=len-4; i++) {
> for (int j=0;j<4;j++) {
> switch(j) {
> case 0:bin.setEndian(bin.BIG_ENDIAN);bin.setSigned(true);
> break;
> case 1:bin.setEndian(bin.LITTLE_ENDIAN);bin.setSigned(true);
> break;
> case 2:bin.setEndian(bin.BIG_ENDIAN);bin.setSigned(false);
> break;
> default:bin.setEndian(bin.LITTLE_ENDIAN);bin.setSigned(false);
> }
> bin.setPos(i);
> tmp = (float) bin.readDWord();
> if (tmp == value)
> System.out.println("pos" + i + " tmp=" + tmp + " ENDIAN=" +
> bin.getEndian() + " signed=" +
> bin.getSigned());
> }
> }
>
> } catch (Exception e) {
> System.out.println("**Error: " + e.getMessage());
> }
>
> }
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads