Problem in conversion binary data in double format

S

scosmo

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
 
P

Pascal J. Bourguignon

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
 
S

scosmo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top