Convert floating point number into hexedecimal
-
Hi Can anyone tell me the mathimatical explanation of converting a floating point to a hexadecimal? Thanks..
It depends on the range of the number you want to represent. I put some basic macros at the end of this message, but the concept that you need to understand is the range of the floating point number and the number of bits in your integer representation (HEX or DECIMAL is really not relevant, they are just ways of viewing the integer). So for example if you are trying to represent PI as a integer number using a 16 bit word, you need at two integer bits for the 3 and the rest could be fractional bits. Assuming an signed 16-bit word, gives you 1 sign bit, 2 integer bits and 13 fractional bits. So multiply PI * 2^13 = 25735.9270. Since this is an integer, your loss of precision is the .9270.... So you are left with 25735 (0x6487). My original PI was 3.1415926535897932384626433832795 If I convert my integer PI back to a floating point number (by dividing by 2^13) we get 3.1414794921875. The important thing to remember when using these converted numbers is to keep track of the decimal location in all of your mathematical operations using that number. If you send me an email address I can send you a couple articles that describe fixed point math operations can concepts. typedef long fixed; // Our new fixed point type. #define itofx(x) ((x) << 8) // Integer to fixed point #define ftofx(x) ((x) * 256) // Float to fixed point #define dtofx(x) ((x) * 256) // Double to fixed point #define fxtoi(x) ((x) >> 8) // Fixed point to integer #define fxtof(x) ((float) (x) / 256) // Fixed point to float #define fxtod(x) ((double)(x) / 256) // Fixed point to double #define Mulfx(x,y) (((y) * (x)) >> 8) // Multiply a fixed by a fixed #define Divfx(x,y) ((y << 8) / (x)) // Divide a fixed by a fixed #define Printfx(x) printf("%ld.%ld", x >> 8, 100 * (unsigned long) ((x) & 0x00ff) >> 8) // Print fixed point. #define NDPrintfx(x) printf("%ld", x >> 8) // Print fixed point without a decimal point.
-
Hi Can anyone tell me the mathimatical explanation of converting a floating point to a hexadecimal? Thanks..
if you can get the binary array of the float, you can easily get its hexadecimal representation... group the bits by groups of 4 bits. then use the following :
binary <--> hexa
0000 0x0
0001 0x1
...
1010 0xA
...
1111 0xFthen, you have it. see this example :
0b 0010 0101 1101 0110
0x 2 5 D 6so, the hexadecimal representation of the bits shown is
0x25d6
... understand ? [edit] ok, i'm quite sure it won't be that ease for you to get the bit array, so here is a sample for that :float floatToAnalyse = 3.45; //for example
char floatBits[32] = "";//not sure here about how to get the binary representation, but this should work
unsigned int uiBitsArray = *((char*)(void*)floatToAnalyse);for (int i = 0; i < 32; i++) {
floatBits[0] = (((uiBitsArray >> (31-i)) & 0x00000001) == 1)
? '1'
: '0';
}[/edit]
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VisualCalc 3.0] -- modified at 8:54 Friday 16th December, 2005