# Conversion of hexadecimal to floating point

Does anybody know any way of conversion of hexadecimal to floating point? Any C, Java, C++ or any language's program that could automatically convert it or the formula to convert it manually on page and copy.

Any help would be highly appreciated.

• what do you mean "convert from hexadecimal to floating point"? "hexadecimal" is a positional numeral system while "floating point" is a representation for real numbers. – Kareem Jun 28 '15 at 13:15

## 1 Answer

In C and C++, the direct conversion goes as follows

``````int i = 0xFF2;
float f = 0xFF2.25p10;
cout<<"i = "<<i<<endl<<"f = "<<f;
``````

Gives output as

``````i = 4082
f = 4.18012e+06
``````

However, this does not highlight the logic behind the conversion, the same can be carried out on pen and paper as follows:

1. Write the whole hexadecimal string on a paper.
2. Write indices below each character of the string. Starting from period (`.`) and going leftwards, assign index 0, 1, 2, and so on. Starting from period and going rightwards, assign index -1, -2, -3, and so on.
3. Using the following mapping, convert each of the characters into their equivalent decimal notation before multiplying in step 4. 4. If `A` is the string that represents your hexadecimal number, then

decimal_equivalent = ∑ A[i]*(16i)
where, i is the index in `A`.

For example, consider the number `FF2.25`,

`````` ----------------------------------------------------
|    F    |    F    |    2     .    2     |    5     | <--- hex number
-----------------------------------------------------
|   2     |    1    |    0     |    -1    |   -2     | <--- index
----------------------------------------------------
|  16**2  |  16**1  |  16**0   | 16**(-1) | 16**(-2) | <--- 16**index
----------------------------------------------------
|  F*256  |  F*16   |   2*1    | 2*0.0625 | 5*0.0039 | <--- A[index]*(16**(index))
----------------------------------------------------
|  15*256 |  15*16  |   2*1    | 2*0.0625 | 5*0.0039 | <--- conversion
----------------------------------------------------
|   3840  +   240   +    2     +   0.125  +  0.0195  =  4082.1445
----------------------------------------------------
``````

Consider the below C++ code snippet as a pseudocode, I haven't tested it(sorry for that).

``````int indexWithPeriod(string s)
{
// returns index at which '.' is stored; else -1
int len = s.length()-1;
while (len--)
{
if (s[len] == '.')
return len;
}
return -1;
}
double hexToDec(string hex)
{
// returns decimal equivalent of a hexa decimal number stored in string
double ans = 0;
int len = hex.length();
int periodAt = indexWithPeriod(hex);
int i = periodAt-1, k = 0;
while (i >= 0)
{
ans += hex[i]*pow(16, k);
--i;
++k;
}
i = periodAt+1;
k = -1;
while (i < len)
{
ans += hex[i]*pow(16, k)
++i;
--k;
}
return ans;
}
``````

The function `hexToDec()` will convert the given hexadecimal number in string `hex` to a double precision floating point number(returning its value).

There is an implicit `pow()` function in `cmath`, and a `length()` function in `cstring`.