Problem in conversion binary data in double format

Discussion in 'C++' started by scosmo@tiscalinet.it, May 26, 2009.

  1. Guest

    Hello,
    I've this "little" problem , I hope someone could help me.
    I'm using g++ on a linux machine.
    I'm reading, from a binary file, the following sequence of data:

    3e 41 89 37

    through the following instructions:

    double y;
    fread(&y,8,1,file);

    the 8 bytes read represents a 8 byte real floating number expressed in
    the following representation:

    SEEEEEEE MMMMMMMM MMMMMMMM …. MMMMMMMM

    Where S is the sign, E the exponent and M the mantissa.

    I do not know if g++ represents double data in the same way... this is
    the real problem.

    The question is: how could I convert the read data into a double
    variable?
    I should obtain a double value of 0.001 from this data.

    Regards,

    camelot
     
    , May 26, 2009
    #1
    1. Advertising

  2. writes:

    > Hello,
    > I've this "little" problem , I hope someone could help me.
    > I'm using g++ on a linux machine.
    > I'm reading, from a binary file, the following sequence of data:
    >
    > 3e 41 89 37
    >
    > through the following instructions:
    >
    > double y;
    > fread(&y,8,1,file);
    >
    > the 8 bytes read represents a 8 byte real floating number expressed in
    > the following representation:
    >
    > SEEEEEEE MMMMMMMM MMMMMMMM …. MMMMMMMM
    >
    > Where S is the sign, E the exponent and M the mantissa.
    >
    > I do not know if g++ represents double data in the same way... this is
    > the real problem.
    >
    > The question is: how could I convert the read data into a double
    > variable?
    > I should obtain a double value of 0.001 from this data.



    Have a look at ldexpf(3).

    If you only have four bytes, you must first convert them to a float
    and then from float to double.

    http://en.wikipedia.org/wiki/IEEE_754
    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, May 26, 2009
    #2
    1. Advertising

  3. Guest

    Thank you, but I think I could use the function ldexpf in order to do
    the inverse operation (Decimal to binary...). Am I wrong?

    Then, for what I want to do, the binary mantissa M is normalized, if I
    have the decimal value of M, what operation I have to do (on the
    decinal number) if I wanto to de-normalize it?

    My purpose is to have value = -1*S*M*2^E

    Thank you,

    Camelot

    >
    > Have a look at ldexpf(3).
    >
    > If you only have four bytes, you must first convert them to a float
    > and then from float to double.
    >
    > http://en.wikipedia.org/wiki/IEEE_754
    > --
    > __Pascal Bourguignon__- Nascondi testo citato
    >
    > - Mostra testo citato -
     
    , May 26, 2009
    #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. Sydex
    Replies:
    12
    Views:
    6,507
    Victor Bazarov
    Feb 17, 2005
  2. Drake

    binary data conversion

    Drake, Apr 11, 2004, in forum: C Programming
    Replies:
    7
    Views:
    541
    Thomas Matthews
    Apr 12, 2004
  3. Alexander Eisenhuth
    Replies:
    5
    Views:
    540
    Bob Gailer
    Jul 25, 2003
  4. J.M.
    Replies:
    5
    Views:
    785
  5. Ken Starks
    Replies:
    4
    Views:
    349
    Ken Starks
    Jun 23, 2008
Loading...

Share This Page