在VB中,日期变量占用4个字节大小,
我想将一个时间变量保存到一个byte型变量数组中,应该如何实现保存和读取呢?

解决方案 »

  1.   

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Dim a As Date
    Dim b(3) As Byte
    Private Sub Form_Load()
    CopyMemory b(0), a, Len(a)
    End Sub
      

  2.   

    不好意思,忘记给a赋值了
    Private Sub Form_Load()
    a = Now
    CopyMemory b(0), a, Len(a)
    End SubCopyMemory 的说明
    The CopyMemory function copies a block of memory from one location to another.?Destination
    Points to the starting address of the copied block抯 destination.?Source
    Points to the starting address of the block of memory to copy.?Length
    Specifies the size, in bytes, of the block of memory to copy.
      

  3.   

    那我如何将数组中的值再读出来保存到Date变量中去呢?
      

  4.   

    反过来啊
    dim c as date
    CopyMemory c, b(0), Len(c)
      

  5.   

    CopyMemory
    楼上该说的都说了,只好来蹭点分
      

  6.   

    bobbyxby(i386) 的代码好像有点问题,应该是这样的: Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Dim a As Date
    Dim b(3) As Byte
    Private Sub Form_Load()
    a=now
    CopyMemory varptr(b(0)), varptr(a), Len(a)
    End Sub
      

  7.   

    不好意思CopyMemory函数中指针是不能按引用传递的,必须安值传递,所以代码应该是这样的Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Dim a As Date
    Dim b(3) As Byte
    Private Sub Form_Load()
    a=now
    CopyMemory byval varptr(b(0)), byval varptr(a), Len(a)
    End Sub另:VB中的数组是SafeArray,它与C中的数组是不一样的,必须用VarPtr函数才能获得b(0)的真正地址
      

  8.   

    Non, non, non!!! A Date var. is 8-bytes long (double)!
    try:
    dim x as date
    debug.print lenb(x)
      

  9.   

    I am very very sorry that our code leads to the same result,i really dont know what's the difference(to James0001(虾米—什么时候成大虾?) )
    lenb and len gets the same val
    try the following samplePrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Dim a As Date
    Dim d As Date
    Dim b(3) As Byte
    Dim c(3) As Byte
    Private Sub Form_Click()
    Me.AutoRedraw = True
    Me.Cls
    a = Now
    CopyMemory ByVal VarPtr(b(0)), ByVal VarPtr(a), LenB(a)
    For i = 0 To 3
    Me.Print b(i)
    Next i
    CopyMemory c(0), a, Len(a)
    For i = 0 To 3
    Me.Print c(i)
    Next i
    CopyMemory d, c(0), LenB(d)
    MsgBox a
    MsgBox d
    MsgBox Len(a)
    MsgBox LenB(a)
    End Subwin2003 vb6 sp6using vb ,you should bypass some boring things or use c
      

  10.   

    谢谢大家,问题我已经解决了,用的是bobbyxby(i386的方法!不好意思,我把Date类型的变量的大小弄错了!非常的感谢bobbyxby(i386,也谢谢大家的参与!散分!!