Velocity Reviews > C++ > Problem in conversion binary data in double format

# Problem in conversion binary data in double format

scosmo@tiscalinet.it
Guest
Posts: n/a

 05-26-2009
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;

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

Pascal J. Bourguignon
Guest
Posts: n/a

 05-26-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) 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;
>
> 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__

scosmo@tiscalinet.it
Guest
Posts: n/a

 05-26-2009
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 -