[Hex to float Conversion]

appdev

New Member
Joined
Apr 27, 2022
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hello, I hope you're doing well.

I'm a new member.

I was wondering how to convert a hexadecimal to floating point in excel.

Can someone help me please ?

I used Hex2DEC but that didn't return a floating point.

Thank you.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
What are you starting with as your hex string? Do you have a hexadecimal point in your string? HEX2DEC only deals with integers.

Here is a VBA function that will return a real number when the hex string has a hex point in it.
VBA Code:
Function HEX2DECFloat(S As String) As Double

   Dim i As Long
   Dim Exp As Double
   
   If InStr(S, ".") = 0 Then
      HEX2DECFloat = CDbl("&H" & S)
   Else
      ' Get the integer part
      HEX2DECFloat = CDbl("&H" & Left(S, InStr(S, ".") - 1))
   
      ' Add the decimal part
      Exp = 16
      For i = InStr(S, ".") + 1 To Len(S)
         HEX2DECFloat = HEX2DECFloat + CDbl("&H" & Mid(S, i, 1)) / Exp
         Exp = Exp * 16
      Next i
      
   End If

End Function

This could be done with formulas but would be rather complicated.
 
Upvote 0
Just for completeness, here is one that goes the other direction. Because a hex fraction may be not be able to be represented exactly in a decimal number, this function includes an optional precision argument. The conversion stops once the hex number is below the decimal number + precision. Otherwise you could get hex numbers that look like

FF.FF000000000000000000000000000000001
VBA Code:
Function DEC2HEXFloat(D As Double, Optional Precision = 0.000000000000001) As String

   Dim Exp As Double
   Dim Resolved As Long
   Dim ResolvedH As String
   Dim DecRemainder As Double
   
   If D = Int(D) Then
      DEC2HEXFloat = Hex(D)
   Else
      ' Get the integer part
      DEC2HEXFloat = Hex(Int(D)) & "."
   
      ' Add the decimal part
      DecRemainder = D - Int(D)
      Exp = 16
      
      Do Until DecRemainder < Precision
      
         Resolved = Int(DecRemainder * Exp)
         ResolvedH = Hex(Resolved)
         DEC2HEXFloat = DEC2HEXFloat & ResolvedH
         
         DecRemainder = DecRemainder - Resolved / Exp
         
         Exp = Exp * 16
         
      Loop
      
   End If

End Function
 
Upvote 0

Forum statistics

Threads
1,221,593
Messages
6,160,694
Members
451,665
Latest member
PierreF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top