Article View: borland.public.jbuilder.java.language
Article #265Problem reading numbers from a binary file
From: 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());
}
}
Technical Details
Message-ID:
<4533c011$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f74b3,9a52b5f13e5dc50e,start X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 16 Oct 2006 13:23:50 -0400 From: Magnus <magnus_bjorkman@yahoo.com> User-Agent: Thunderbird 1.5.0.7 (Windows/20060909) MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Problem reading numbers from a binary file Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.149.234.69 Message-ID: <4533c011$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1161019409 24.149.234.69 (16 Oct 2006 10:23:29 -0700) Lines: 61 Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail