将Double转换为8字节数组

时间:2011-10-18 00:13:15

标签: vb6

我想将Double变量转换为8字节数组,这是我到目前为止所提供的:

Dim b(0 To 7) As Byte
Dim i As Integer

dim d as double
d = 1            ' for simplicity, I sit the variable "d" to 1

For i = 0 To 7
    Call CopyMemory(b(i), ByVal VarPtr(d) + i, 1)
Next i

' b => [0, 0, 0, 0, 0, 0, 240, 63]

我做错了什么?

2 个答案:

答案 0 :(得分:5)

不要使用循环,请使用length参数:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByRef Destination As Any, _
    ByRef Source As Any, _
    ByVal Length As Long)

Sub DblToByte(ByVal D As Double)
    Dim Bytes(LenB(D) - 1) As Byte
    Dim I As Integer
    Dim S As String

    CopyMemory Bytes(0), D, LenB(D)

    For I = 0 To UBound(Bytes)
        S = S & CStr(Bytes(I)) & " "
    Next
    MsgBox S
End Sub

Private Sub Form_Load()
    DblToByte 1
    Unload Me
End Sub

答案 1 :(得分:3)

您没有显示您的声明语句,但可以针对它的不同用途声明CopyMemory。尝试:

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
                                                 ByVal pDst As Long, _
                                                 ByVal pSrc As Long, _
                                                 ByVal ByteLen As Long _
                                                 )