Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Double into 8-bytes array

Tags:

vb6

I want to convert a Double variable into an 8-bytes array, this is what I've come with so far:

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]

What I'm doing wrong?

like image 228
Ken D Avatar asked Mar 25 '26 10:03

Ken D


1 Answers

Don't use a loop, use the length argument:

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
like image 184
Bob77 Avatar answered Mar 27 '26 06:03

Bob77



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!