Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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;
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
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      05-26-2009
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__
 
Reply With Quote
 
 
 
 
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 -


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
binary number format ? format character %b or similar. Ken Starks Python 4 06-23-2008 08:59 AM
conversion double* to vector<double> and vice versa J.M. C++ 5 03-08-2007 03:45 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM
binary data conversion Drake C Programming 7 04-12-2004 04:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57