This is a modification of Confounded's excellent answer. I modified their function to be use the built-in function Hex rather than bit-wise operations to get the to the bit patterns, made it be able to handle both single and double precision flexibly, and return either the results in either hex (the default) or binary:
Type TDouble
Value As Double
End Type
Type TSingle
Value As Single
End Type
Type DArray
Value(1 To 8) As Byte
End Type
Type SArray
Value(1 To 4) As Byte
End Type
Function DoubleToArray(DPFloat As Double) As Variant
Dim A As TDouble
Dim B As DArray
A.Value = DPFloat
LSet B = A
DoubleToArray = B.Value
End Function
Function SingleToArray(SPFloat As Single) As Variant
Dim A As TSingle
Dim B As SArray
A.Value = SPFloat
LSet B = A
SingleToArray = B.Value
End Function
Function HexToBin(hDigit As String) As String
Select Case hDigit
Case "0": HexToBin = "0000"
Case "1": HexToBin = "0001"
Case "2": HexToBin = "0010"
Case "3": HexToBin = "0011"
Case "4": HexToBin = "0100"
Case "5": HexToBin = "0101"
Case "6": HexToBin = "0110"
Case "7": HexToBin = "0111"
Case "8": HexToBin = "1000"
Case "9": HexToBin = "1001"
Case "A": HexToBin = "1010"
Case "B": HexToBin = "1011"
Case "C": HexToBin = "1100"
Case "D": HexToBin = "1101"
Case "E": HexToBin = "1110"
Case "F": HexToBin = "1111"
End Select
End Function
Function ByteToString(B As Byte, Optional FullBinary As Boolean = False)
Dim BitString As String
BitString = Hex(B)
If Len(BitString) < 2 Then BitString = "0" & BitString
If FullBinary Then
BitString = HexToBin(Mid(BitString, 1, 1)) & HexToBin(Mid(BitString, 2, 1))
End If
ByteToString = BitString
End Function
Function FloatToBits(float As Variant, Optional FullBinary As Boolean = False) As String
Dim ByteArray() As Byte
Dim BitString As String
Dim i As Integer, n As Integer
Dim x As Double, y As Single
If TypeName(float) = "Double" Then
n = 8
x = float
ByteArray = DoubleToArray(x)
ElseIf TypeName(float) = "Single" Then
n = 4
y = float
ByteArray = SingleToArray(y)
Else
FloatToBits = "Error!"
Exit Function
End If
For i = n To 1 Step -1
BitString = BitString & ByteToString(ByteArray(i), FullBinary)
Next i
FloatToBits = BitString
End Function
Here is a test:
Sub test()
Dim x As Single, y As Double
x = Application.WorksheetFunction.Pi()
y = Application.WorksheetFunction.Pi()
Debug.Print FloatToBits(x)
Debug.Print FloatToBits(x, True)
Debug.Print FloatToBits(y)
Debug.Print FloatToBits(y, True)
End Sub
Output:
40490FDB
01000000010010010000111111011011
400921FB54442D18
0100000000001001001000011111101101010100010001000010110100011000
When I feed 400921FB54442D18 into this online tool I get back 3.141592653589793, which makes perfect sense.
Somewhat curiously, when I apply this to 10.4 I get
0100000000100100110011001100110011001100110011001100110011001101
which differs in the final place from the example in this excellent article on floats in Excel VBA. Both versions round to 10.4 (to many, many places). I don't quite know what to make of the discrepancy.
frexpfunction in VBA?”. Googling for this does not turn up any immediate solution hidden in VBA's standard library, but does turn up some attempts are implementing this functionality with floating-point operations. You could get inspiration from these. It just seems like VBA is the wrong language to access the bits of the representation of a floating-point number. - Pascal Cuoq