Conversion Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, here is what I'm doing. I've got 4 bytes that represent a number in IEEE
format. I'm placing them in a double variable like so:
double myDouble = byte[0] << 24 | byte[1] << 16 | byte[2] << 8 | byte[3];
Since the double data type is an IEEE format, shouldn't the variable now be
a floating point number? When I print it out I get a large integer number.
What is happening here? Thanks.
 
JRB said:
Hi, here is what I'm doing. I've got 4 bytes that represent a number in IEEE
format. I'm placing them in a double variable like so:
double myDouble = byte[0] << 24 | byte[1] << 16 | byte[2] << 8 | byte[3];
Since the double data type is an IEEE format, shouldn't the variable now be
a floating point number? When I print it out I get a large integer number.
What is happening here?

What's happening is that you're shifting a lot of bytes, and getting an
integer. There's nothing in the RHS to suggest that there should be any
floating point arithmetic involved.

Have a look at BitConverter.ToDouble to solve your problem - but try to
understand why your solution didn't work, first.

Jon
 
JRB,
double myDouble = byte[0] << 24 | byte[1] << 16 | byte[2] << 8 | byte[3];
Since the double data type is an IEEE format, shouldn't the variable now
be
a floating point number?

No way! You are getting an integer value corresponding to the INTERNAL
REPRESENTATION of the floating point number, which has nothing to do with
the floating point number itself.

As John said, use the BitConverter class.

BTW, if you have four bytes, it is a float, not a double.

Regards - Octavio


When I print it out I get a large integer number.
 

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

Back
Top