将VB Double转换为Pascal Real [英] Convert VB Double to Pascal Real
问题描述
我正在编写一个程序,使用VB.NET需要与DOS进行通信,而不是无法修改的
Pascal程序。通信渠道是通过某些文件数据库来完成的,我将VB Double值写入
这个文件有一个很大的问题,因为Pascal程序可以将它们读作Pascal Real值。
我已经设法找到算法来读取Pascal Real格式和
将它转换为VB Double,但我无法弄明白相反的算法。
有人可以帮我扭转我的算法并开发函数
DoubleToReal(ByVal Data As Double)As String
这是从真实到双倍的转换:
公共函数RealToDouble(ByVal Data As String)As Double
Dim dMantissa作为Double
Dim i As Integer
Dim j As Long
Dim k As Long
如果Len(数据)<> 6然后
''Err.Raise
''例外
退出功能
结束如果
''累积尾数
dMantissa = 1
for i = 6 To 2 Step -1
For j = CType(IIf(i = 6,6,7),长)到0步-1
k = k + 1
If(Asc(Mid $(数据) ,i,1))和CType(2 ^ j,Long))<> 0然后
dMantissa = dMantissa + 2 ^ -k
结束如果
下一个j
下一个我是谁/>
''最后,将所有部分组合成一个数字
If(Asc(Mid $(Data,6,1))和& H80)=& ; H80然后
RealToDouble = -dMantissa * 2 ^(Asc(Mid $(数据,1,1)) - 129)
Else
RealToDouble = dMantissa * 2 ^(Asc(Mid $(数据,1,1)) - 129)
结束如果
尝试
返回([Decimal] .Round(CDec(RealToDouble),2))
Catch ex As Exception
''MsgBox(" RealToDouble,Conversion error :&&;& Data&";"&
RealToDouble.ToString,MsgBoxStyle.Critical)
返回0
结束试试
结束功能
感谢您的帮助
David
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
推荐答案
(Data,i,1))和CType(2 ^ j,Long))<> 0然后
dMantissa = dMantissa + 2 ^ -k
结束如果
下一个j
下一个我是谁/>
''最后,将所有部分组合成一个数字
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
(数据,6, 1))和& H80)=& H80然后
RealToDouble = -dMantissa * 2 ^(Asc(Mid
(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid
(数据,1,1) ) - 129)
Else
RealToDouble = dMantissa * 2 ^(Asc(Mid
(Data, 1, 1)) - 129)
Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid
这篇关于将VB Double转换为Pascal Real的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!