不用COPYMEMORY函数,如何将双精度浮点数存储到数组中
本帖最后由 manyclient 于 2012-10-12 08:51:27 编辑 不用COPYMEMORY函数
1、如何将双精度浮点数存储到数组中
2、转存后的数组,如何再得到这个浮点数。
[最优解释]
VB对位的操作是非常弱的,而且double是有固定格式的,并不能像int或long能通过乘以或除以2的次幂来模拟移位而得到各字节,因此操作内存是难免的,不过如不想用CopyMemory函数,你也可以使用VB的LSet语句来完成,示例:
Option Explicit
Private Type tmpDouble '用自定义类型把Double封起来,因为LSet只能使用自定义类型,下同
a As Double
End Type
Private Type tmpByteArray
a(8) As Byte
End Type
Private Sub Command1_Click()
Dim a As tmpDouble, b As tmpByteArray, c As tmpDouble, d As Double, e(8) As Byte
a.a = 120.5
LSet b = a 'Double送到字节数组
Dim i As Integer
For i = 0 To 7
Debug.Print a.a & "的第" & i + 1 & "个字节是:" & "&H" & Hex(b.a(i))
Next
LSet c = b '字节数组送到Double
Debug.Print c.a
End Sub
Sub temp()
Dim i As Integer
Dim d As Double
Dim s As String
Dim strArray() As String
'将双精度存放到数组中
d = 123.45
s = CStr(d)
ReDim strArray(Len(s))
For i = 1 To Len(s)
strArray(i - 1) = Mid(s, i, 1)
Next
'从数组中恢复双精度
s = Join(strArray, "")
d = CDbl(s)
Debug.Print d
End Sub