Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Reading Delphi binary floats

Thread view: 
MB - 17 Oct 2006 16:50 GMT
Hi,
I have a binary file generated by a Delphi program. I can read all
strings (prefix length strings) but I can't get the float right.

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.

Any an all help is much appreciated
EJP - 18 Oct 2006 01:46 GMT
> 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 don't know what that last sentence means, but 00 00 20 42 looks like a
word-swap. Try swapping it around to 20 42 00 00, then if that doesn't
work try swapping the bytes too. There are a lot of permutations. If you
really want to get this right, find the specification of the Delphi
single-precision FP and work out how to map it to IEEE 754 which is what
Java uses.
Arne Vajhøj - 18 Oct 2006 02:06 GMT
> I have a binary file generated by a Delphi program. I can read all
> strings (prefix length strings) but I can't get the float right.
[quoted text clipped - 8 lines]
> I have tried any type of read and conversion but can't get this one
> right (or any floats) from the file.

import java.io.*;

public class FP {
   public static void main(String[] args) throws IOException {
       byte[] b = { 0x42, 0x20, 0x00, 0x00 };
       DataInputStream dis = new DataInputStream(new
ByteArrayInputStream(b));
       float x = dis.readFloat();
       System.out.println(x);
   }
}

outputs 40.0 !

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.