#### Archived

This topic is now archived and is closed to further replies.

# Hex to float

This topic is 6265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, Im having a little trouble working something out and i was wondering if you wonderfull people could help. I want to know how to convert a hexidecimal value into a float, so that when i look at the memory watch window i can calculate the float value. Thanks guys!!

##### Share on other sites
Just like a base 10 number to float:

104 | 103 | 102 | 101 | . | 10-1 | 10-2 | 10-3 |

164 | 163 | 162 | 161 | . | 16-1 | 16-2 | 16-3 |

So I'd say as long as you can find out just how your system displays fractional parts of hex numbers, you can walk through the conversion one digit at a time, if you like.

----------------
-WarMage
...hope the tag works...

Edited by - WarMage on September 20, 2001 12:31:31 PM

##### Share on other sites
So how do i find out where the floating point is?? i use Visual Studio if that makes any difference!!

Cheers

##### Share on other sites
that''s totally and completely 100% wrong.

you need to follow the IEEE standard to convert a "hex" number (ie, bits) to a "float". it''s significantly more complicated than what was posted above.

type the following in quickwatch

"*(float*)0x00500000"

this casts 0x00500000 to a pointer to a float and then de-references the address to yield the float.

##### Share on other sites
quote:
Original post by Anonymous Poster
that''s totally and completely 100% wrong.

you need to follow the IEEE standard to convert a "hex" number (ie, bits) to a "float". it''s significantly more complicated than what was posted above.

type the following in quickwatch

"*(float*)0x00500000"

this casts 0x00500000 to a pointer to a float and then de-references the address to yield the float.

...yes, that is a more complicated explanation, but mine was not "completely 100% wrong". I just didn''t give a code example.

------------
-WarMage
...so don''t start a flame war where there needn''t be one...

##### Share on other sites
I have been using the *(float*) cast up until now, but i wanted to be able to decipher the code maually.

The base 16 method is not a problem either, but where is the floating point defined ??? That was the real problem

Thanks for the help so far tho guys

##### Share on other sites
quote:
Original post by WarMage
...yes, that is a more complicated explanation, but mine was not "completely 100% wrong". I just didn't give a code example.

Actually, it was about 80% wrong. Floats and doubles are represented in mantissa/exponent format. What you described was scaled fixed point format, which is often used in DSPs and fixed-point graphics engines. The two look nothing alike.

To answer spider11, C represents floating-point numbers like this: m*2^e
...where m is the mantissa, and e is the exponent. floats and doubles assign different numbers of bits to the m and e fields. If you figure that out, the 2^e is where your point is. It's free to jump all over the place. I believe there's also a sign bit at the front.

Edited by - Stoffel on September 20, 2001 2:22:53 PM

##### Share on other sites
Not sure if this is what you need, but...

I''m working on an app that receives a byte stream. Some of those bytes group to be floats, ints, custom data typed, etc.

Since I didn''t want to figure out how to calculate a float given four bytes, I create a simple union

typedef unsigned char byte;struct{    union    {        byte bytes[4];    }    float fValue;} byte_or_float;

Now, all I did was
// Assume that stream is a byte arraybyte_or_float foo;foo.bytes[0] = stream[0];foo.bytes[1] = stream[1];foo.bytes[2] = stream[2];foo.bytes[3] = stream[3];// Now use fooSomeFunctionThatTakesFloats (foo.fValue);

If you don''t want to always specify the .fValue part, you could exted it by implementing a float operator, something like:
struct{    union    {        byte bytes[4];    }    float fValue;    operator float() { return fValue; };} byte_or_float;// You can just pass it in like a floatSomeFunctionThatTakesFloats (foo);

Hope that helped

---
PAGE FAULT: Please insert "Swap File, Disk 2"
and press any key to continue.

##### Share on other sites
Thats quite a cool idea,

It doesn not matter in the end anyway, as i just realised that while i was expecting much larger numbers, the ones i was geting are perfectly reasonable, i wanted to look around the memory and try to see if i was in the wrong place in the buffer by hand would have ony been a few bytes out either way (last resort i know, but it has worked for me twice now with chars and ints)

Cheers all anyway!!

##### Share on other sites
quote:
Original post by Stoffel
[quote]Original post by WarMage
...yes, that is a more complicated explanation, but mine was not "completely 100% wrong". I just didn''t give a code example.

Actually, it was about 80% wrong

Well, based on your post, Stoffel, I can''t argue - nor would I. I was aware that the storage process is different between systems, and alluded to such in my post, but I guess that''ll teach me to be lazy about writing a post, hmm?

-------------
-WarMage
...ergh...

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633319
• Total Posts
3011346
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!