Reading Delphi binary floats

Discussion in 'Java' started by MB, Oct 17, 2006.

  1. MB

    MB Guest

    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
    MB, Oct 17, 2006
    #1
    1. Advertising

  2. MB

    EJP Guest

    MB wrote:

    > 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.
    EJP, Oct 18, 2006
    #2
    1. Advertising

  3. MB wrote:
    > 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.


    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
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kartik
    Replies:
    2
    Views:
    288
    Grant Edwards
    Feb 1, 2005
  2. Replies:
    2
    Views:
    514
  3. reading Java floats from C

    , Jun 20, 2005, in forum: C Programming
    Replies:
    6
    Views:
    384
    Robert Maas, see http://tinyurl.com/uh3t
    Jun 29, 2005
  4. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,234
    Tim Rentsch
    Sep 23, 2005
  5. Alagalah
    Replies:
    1
    Views:
    316
    John Machin
    Jul 15, 2009
Loading...

Share This Page