vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
NEU! sevCoolbar 3.0 - Professionelle Toolbars im modernen Design!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2025
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

Fortgeschrittene Programmierung
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln 
Autor: Wöllmi
Datum: 26.09.06 16:39

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

alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
Gleitpunktzahl / Gleitkommazahl in HEX wandeln3.538bluesky8126.09.06 07:24
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.553AndyOG26.09.06 08:01
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.514Dirk26.09.06 08:17
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.678bluesky8126.09.06 08:22
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.475Dirk26.09.06 08:32
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.485Dirk26.09.06 08:53
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.675Wöllmi26.09.06 16:39
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.616Dirk27.09.06 13:39
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.472bluesky8109.10.06 15:25
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.641Zardoz09.10.06 16:11
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.432bluesky8109.10.06 17:43
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.728Zardoz09.10.06 18:06
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.398bluesky8109.10.06 18:16
Re: Gleitpunktzahl / Gleitkommazahl in HEX wandeln2.534Zardoz09.10.06 18:39

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2025 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel