Article View: borland.public.jbuilder.java.language
Article #266Re: Problem reading numbers from a binary file
From: 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());
> }
>
> }
Technical Details
Message-ID:
<4536292e$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-mailReferences:
<4533c011$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,9a52b5f13e5dc50e X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Wed, 18 Oct 2006 09:16:55 -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: Re: Problem reading numbers from a binary file References: <4533c011$1@newsgroups.borland.com> In-Reply-To: <4533c011$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.149.234.69 Message-ID: <4536292e$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1161177390 24.149.234.69 (18 Oct 2006 06:16:30 -0700) Lines: 91 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