I would like to convert hexadecimal integer, for example "ABE46D41" to a float ("ABE46D41" should be ~14.87).
Is this kind of convertion possible?
Because floats are stored like this in the memory...
I would like to convert hexadecimal integer, for example "ABE46D41" to a float ("ABE46D41" should be ~14.87).
Is this kind of convertion possible?
Because floats are stored like this in the memory...
Where do you get this from? A string?
Try something like that.Code:#include <sstream> #include <iomanip> std::string input = "ABE46D41"; std::istringstream iss(input) >> hex >> d;
On the other hand, you can also store numbers as hex:
Provided that is a real hex number of course.Code:double d = 0xABE46D41;
Thanks for help.
Last edited by maxorator; 06-25-2006 at 11:10 AM.
But I get those numbers directly as integers (or DWORD-s)...
Please post what relevant code you have.
maxorator: It sounds like you are trying to convert the bytes at a memory location into a float value. There are a number of gotchas with this, but this seems like what you are trying to do.Code:#include <iostream> int main() { unsigned long value = 0x416DE4ABUL; float result = *reinterpret_cast<float*>(&value); std::cout << "value = " << std::hex << value << ", result = " << result << "\n"; unsigned char bytes[] = {0xAB,0xE4,0x6D,0x41}; result = *reinterpret_cast<float*>(bytes); std::cout << "result = " << result << "\n"; return 0; } /* my output value = 416de4ab, result = 14.8683 result = 14.8683 */
Last edited by Dave_Sinkula; 06-25-2006 at 08:57 PM. Reason: Twiddled code a couple times.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I already found one from google, something like:
And I made a function to use with it:Code:union { int i; float f; } totfloat;
Thanks for the helpCode:float tofloat(int data){ totfloat.i=data; return totfloat.f; }
This does not convert an int into a float. The union is a way of viewing the same piece of memory in different ways but the content of that memory does not change.Originally Posted by maxorator
Your tofloat() function stores a bit pattern that represents an integral number of a certain value. That integer is most likely 32 bits long, i.e. four bytes, but could be 16 bits or 64 bits depending on your platform. It then returns that bit pattern pretending that it is a float. It isn't; floats are stored with three components, viz: sign, exponent and mantissa.
Actually, the compiler will do this conversion for you; all you need do is assign the int value into the float.
That appears to be the point. And yes, there are gotchas with this.Originally Posted by risby
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
risby, how else would you convert ABE46D41 to something near 14.87?
This IS what I wanted to do.
Following your advice would've made up 2883874113.00f out of ABE46D41.
And another question - How is float built up exactly? I want to know a way to convert it into float manually. I like knowing these things
Last edited by maxorator; 06-27-2006 at 09:01 AM.
I am very sorry max. I was sleeping when I read the thread!Originally Posted by maxorator
I realise now that you have binary floats stored on disk or summat. Can you not simply read them in as floats? Why do they exist in integers at the point you need them?
I never even thought of reading them in as floats... good idea
Still, could anyone answer my other questions:
How is float built up exactly? I want to know a way to convert it into float manually
Did you follow the link I posted? And no, you probably don't want "to convert it into float manually".
What's the "big picture" of what you are trying to do?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
That is an excellent link Dave. Max, I guess you may not have realised it was a link as it was so neatly formatted; raw it is http://en.wikipedia.org/wiki/IEEE_754Originally Posted by maxorator
As well as diagrams it even has a C function to convert 32 bits into a float. You'll have to be really careful that your own compiler conforms exactly to the standard which the wiki article describes if you use the function through.