D
David Scemama
Hi,
I'm writing a program using VB.NET that needs to communicate with a DOS
Pascal program than cannot be modified. The communication channel is through
some file databases, and I have a huge problem writing VB Double values to
the file so as the Pascal program can read them as Pascal Real values.
I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite algorithm.
Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"
Here is the conversion from real to double:
Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long
If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If
'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid$(Data, i, 1)) And CType(2 ^ j, Long)) <> 0 Then
dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i
'finally, assemble all the pieces into a number
If (Asc(Mid$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid$(Data, 1, 1)) - 129)
Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid$(Data, 1, 1)) - 129)
End If
Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try
End Function
Thanks for your help
David
I'm writing a program using VB.NET that needs to communicate with a DOS
Pascal program than cannot be modified. The communication channel is through
some file databases, and I have a huge problem writing VB Double values to
the file so as the Pascal program can read them as Pascal Real values.
I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite algorithm.
Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"
Here is the conversion from real to double:
Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long
If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If
'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid$(Data, i, 1)) And CType(2 ^ j, Long)) <> 0 Then
dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i
'finally, assemble all the pieces into a number
If (Asc(Mid$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid$(Data, 1, 1)) - 129)
Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid$(Data, 1, 1)) - 129)
End If
Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try
End Function
Thanks for your help
David