Hi Bluesky81,
versuch es mal hiermit.
Das ganze ist ausgelegt für Single-Float 32-Bit
API-Declaration in einem Modul:
Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, _
ByVal Length As Long) Umwandlungsfunktionen in einem Modul
Function fkt_sIeeeGz2Hex(sglValue As Single) As String
'Konvertierung von Gleitkommazahl
'in Hexadezimal nach IEEE
'Alles in VB!
Dim sglFloatVal As Single
Dim lngHexVal As Long
sglFloatVal = sglValue
' Ziel Quelle
CopyMemory lngHexVal, ByVal VarPtr(sglFloatVal), 4
fkt_sIeeeGz2Hex = Hex(lngHexVal)
End Function Function fkt_sIeeeHex2Gz(lngValue As Long) As String
'Konvertierung von Hexadezimal
'in Gleitkommazahl nach IEEE
'Alles in VB!
Dim lngHexVal As Long
Dim sglFloatVal As Single
lngHexVal = lngValue
' Ziel Quelle
CopyMemory sglFloatVal, ByVal VarPtr(lngHexVal), 4
fkt_sIeeeHex2Gz = CStr(sglFloatVal)
End Function Aufruf in der Form:
Private Sub cmdHex2Float_Click()
'Konvertierung von Hexadezimal
'in Gleitkommazahl nach IEEE
'Alles in VB
On Error Resume Next
lblHex2Float.Caption = fkt_sIeeeHex2Gz(CLng("&h" & txtHexVal.Text))
If Err.Number <> 0 Then
MsgBox "Formatfehler!: " & CStr(Err), vbOKOnly, "Fehler!"
Err.Clear
End If
End Sub Beispiel: 418A0000 => 17,25
Private Sub cmdFloat2Hex_Click()
'Konvertierung von Gleitkommazahl
'in Hexadezimal nach IEEE
'Alles in VB
On Error Resume Next
lblFloat2Hex.Caption = fkt_sIeeeGz2Hex(CSng(txtSingleFloatVal.Text))
If Err.Number <> 0 Then
MsgBox "Formatfehler!: " & CStr(Err), vbOKOnly, "Fehler!"
Err.Clear
End If
End Sub Beispiel: 17,25 => 418A0000
Achtung!
Auf Sonderwerte habe ich keine Rücksicht genommen! Siehe:
'Kurzdokumentation - Single Precision - Gleitkomma-Zahlenformat
'------------------------------------------------------------------------------
' ---------------------------------------------------------------------
'The IEEE single precision floating point standard representation requires a 32
' bit word, which may be represented as numbered from 0 to 31, left
'to right. The first bit is the sign bit, S, the next eight bits are the
' exponent bits, 'E', and the final 23 bits are the fraction 'F':
'
' S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
' 0 1 8 9 31
'
'The value V represented by the word may be determined as follows:
'
'If E=255 and F is nonzero, then V=NaN ("Not a number")
'If E=255 and F is zero and S is 1, then V=-Infinity
'If E=255 and F is zero and S is 0, then V=Infinity
'If 0<E<255 then V=(-1)**S * 2 ** (E-127) * (1.F) where "1.F" is intended to
' represent the binary number created by prefixing F with an implicit
' leading 1 and a
' binary point.
'If E=0 and F is nonzero, then V=(-1)**S * 2 ** (-126) * (0.F) These are
' "unnormalized" values.
'If E=0 and F is zero and S is 1, then V=-0
'If E=0 and F is zero and S is 0, then V=0
'In particular,
'
' 0 00000000 00000000000000000000000 = 0
' 1 00000000 00000000000000000000000 = -0
'
' 0 11111111 00000000000000000000000 = Infinity
' 1 11111111 00000000000000000000000 = -Infinity
'
' 0 11111111 00000100000000000000000 = NaN
' 1 11111111 00100010001001010101010 = NaN
'
' 0 10000000 00000000000000000000000 = +1 * 2**(128-127) * 1.0 = 2
' 0 10000001 10100000000000000000000 = +1 * 2**(129-127) * 1.101 = 6.5
' 1 10000001 10100000000000000000000 = -1 * 2**(129-127) * 1.101 = -6.5
'
' 0 00000001 00000000000000000000000 = +1 * 2**(1-127) * 1.0 = 2**(-126)
' 0 00000000 10000000000000000000000 = +1 * 2**(-126) * 0.1 = 2**(-127)
' 0 00000000 00000000000000000000001 = +1 * 2**(-126) *
' 0.00000000000000000000001 = 2**(-149) (Smallest positive value)
'
'------------------------------------------------------------------------------
' --------------------------------------------------------------------- Tschaui
Woellmi  |