user3525846
user3525846

Reputation: 33

Converting Hex Dump to Double

MY program currently prints a hex dump by reading from memory where a double is stored.

It gives me

00 00 00 00 00 50 6D 40

How can I make sense of this and get the value I store, which is 234.5?

I realize there are 64 bits in a double, first bit is the sign bit, the next 11 are exponent and the last 52 are the mantissa

(-1)^sign * (1.mantissa) * 2^(exponent - 1023)

However, I've tried both little endian and big endian representations of the double and I can't seem to make it work.

Upvotes: 1

Views: 971

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308432

First thing to realize is that most modern processors use little endian representation. This means that the last byte is actually the most significant. So your value taken as a single hex constant is 0x406d500000000000.

The sign bit is 0. The next 11 bits are 0x406. The next 52 are 0xd500000000000.

(-1)^sign is 1. 2^(exponent - 1023) is 128. Those are simple.

1.mantissa is hard to evaluate unless you realize what it really means. It's the constant 1.0 followed by the 52 bits of mantissa as a fraction. To convert from an integer to a fraction you need to divide it by the representation of 2^52. 0xd500000000000/(2**52) is 0.83203125.

Putting it all together: 1 * (1.0 + 0.83203125) * 128 is 234.5.

Upvotes: 2

user2836797
user2836797

Reputation:

This online calculator can do it for you.

If you are on big endianness, enter

00 00 00 00 00 50 6D 40 

or if you are on little endianness

40 6D 50 00 00 00 00 00

The first is a strange number, the second is 234.5

Upvotes: 0

Related Questions