David G
Code: Select all
Sub FloatToDec (ZxString$, DecNumber#)
Dim FirstByte As Double
Dim ZxDigits As Double
Dim SecondByte As Double
Dim t As Double
Dim Xdigits As Double
Dim ZxSign As Double
Dim figure As Double
zero$ = String$(5, Chr$(0))
If ZxString$ = zero$ Then DecNumber# = 0: Exit Sub
'get sign from second byte
SecondByte = Asc(Mid(ZxString$, 2, 1))
ZxSign = SecondByte And 128
'Adjust second byte after getting sign
Mid(ZxString$, 2, 1) = Chr$(SecondByte Or 128)
'drop trailing bytes that equal 0
'result in x$
For f = 5 To 2 Step -1
If Asc(Mid(ZxString$, f, 1)) Then
Exit For
End If
Next f
x$ = Mid(ZxString$, 2, f - 1)
'get # of binary digits needed from first byte
FirstByte = Asc(Mid(ZxString$, 1, 1))
ZxDigits = FirstByte - 128
'drop trailing bits that equal 0
DecNumber# = Asc(Right(x$, 1))
For f = 8 To 1 Step -1
If DecNumber# And 1 Then Exit For
Let DecNumber# = DecNumber# / 2
Next f
'f = number of significant bits
'get # of binary digits in base number
Xdigits = (Len(x$) - 1) * 8 + f
'convert string to base number
t = 0 'counter for powers of figure (256)
figure = 2 ^ f
For f = (Len(x$) - 1) To 1 Step -1
DecNumber# = DecNumber# + Asc(Mid(x$, f, 1)) * figure * (256 ^ t)
t = t + 1
Next f
'based on exponent, modify number to correct number
'of binary digits
Let DecNumber# = DecNumber# * 2 ^ (ZxDigits - Xdigits)
If ZxSign Then Let DecNumber# = -DecNumber#
End Sub