structure与byte数组相互转换

Public Type uS
    a As Long
    b As Double    
    c As String * 48
End Type

Public Data1() As uS
Public outArray() as byte

如果将 data1转换成 byte数组呢?
还有如何恢复byte数组到 data1中呢?
——————————————————————————

用CopyMemory来做.

例子:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type uS
    a As Long
    b As Double
    c As String * 48
End Type


Private Sub Command1_Click()
Dim Data1  As uS
Dim outArray() As Byte

Data1.a = 5
Data1.b = 8
Data1.c = "aa"

ReDim outArray(Len(Data1))
CopyMemory outArray(0), Data1, Len(Data1) '将结构的信息传入字节数组中.

MsgBox UBound(outArray)
MsgBox outArray(0)

Dim abc As uS
CopyMemory abc, outArray(0), UBound(outArray) '将字节数组还原到结构中

MsgBox abc.c
End Sub

阅读更多
个人分类: VB
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭