Before you think you have the correct solution, please test it with this webpage first:

http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex32.html
And, test your routine with this hex value: 4287A929 which SHOULD be converted to this decimal value: 67.830391 but my routine, and all those I've seen on the web, get 125.2863 instead.

I _have_ seen some of the other routines available, and they all do NOT work with this particular value. Why not? The one by _pit_ here on experts-exchange only accepts 4-byte hex values, and I'm not sure how to "fix" it to accept 8-byte hex values, so I don't even know if his works properly either.

Here is the code I am using. Can anyone please tell me what the bug is...why won't it convert the above hex value to the above correct decimal value? This is not my code, it is the code that has been floating (pardon the pun) around the web for years:

'

' function is getting passed an 8-byte IEEE hex value such as 4287A929

' and is supposed to return a value of 67.83039 but doesn't

'

Public Function SingleConv(whole As Variant) As Variant

Dim expnt, mantis, Psign As Boolean, result, multiplier, L, q, que

L = Len(whole)

If L < 8 Then

whole = whole & String(8 - L, &H30) '"0"

End If

If L > 8 Then

whole = Left(whole, 8)

End If

expnt = "&H" & Left(whole, 3)

If expnt And &H800 Then Psign = True Else Psign = False

expnt = (Val(expnt) And &H7F8) / 8 'shift right 3 places

expnt = "&H" & Hex(expnt)

mantis = "&H" & Right(whole, 6)

mantis = (Val(mantis) And &H7FFFFF) * 2 'convert to numerical and switch top bit off, shift left 1 position

mantis = Hex(mantis) 'stick back into var as hex

expnt = Val(expnt)

'translate expnt

expnt = expnt - &H7F

expnt = 2 ^ expnt

'translate mantis

result = 1

For q = 1 To 6

que = "&H" & Mid(mantis, q, 1)

que = Val(que)

multiplier = 1 / 2 ^ (q * 4)

result = result + (multiplier * que)

Next q

result = result * expnt

If Psign Then result = 0 - result

SingleConv = str(result)

End Function

The hex data 4287A929 is also 4 bytes.

Converting a 4 byte hex to a Single should be easy:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()

Dim sngVal As Single

Dim sHex As String

Dim lVal As Long

'Our Hex String

sHex = "4287A929"

'The value of the hex string

lVal = Val("&h" & sHex)

'Copy the bytes over to a Single data type

Call CopyMemory(sngVal, lVal, 4)

'Display the results

MsgBox Format(sngVal, "0.000000")

End Sub