byte数组和其他数据类型之间的转化
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim test(0 To 3) As Byte
Dim i As Long, j As Long
i = 1000
'从long转化为byte数组
CopyMemory test(0), i, 4
'从byte数组转化为long
CopyMemory j, test(0), 4
MsgBox j
End Sub
或者根据下面的代码中的函数进行转换:
Public Function HiByte(ByVal wParam As Integer) As Byte
'note: VB4-32 users should declare this function As Integer
HiByte = (wParam And &HFF00&) / (&H100)
End Function
Public Function LoByte(ByVal wParam As Integer) As Byte
'note: VB4-32 users should declare this function As Integer
LoByte = wParam And &HFF&
End Function
Public Function HiWord(wParam As Long) As Integer
If wParam And &H80000000 Then
HiWord = (wParam / 65535) - 1
Else
HiWord = wParam / 65535
End If
End Function
Public Function LoWord(wParam As Long) As Integer
If wParam And &H8000& Then
LoWord = &H8000& Or (wParam And &H7FFF&)
Else
LoWord = wParam And &HFFFF&
End If
End Function
Public Function LoWordCM(wParam As Long) As Integer
'using API
CopyMemory LoWordCM, wParam, 2
End Function
Public Function LShiftWord(ByVal w As Integer, ByVal c As Integer) As Integer
Dim dw As Long
dw = w * (2 ^ c)
If dw And &H8000& Then
LShiftWord = CInt(dw And &H7FFF&) Or &H8000&
Else
LShiftWord = dw And &HFFFF&
End If
End Function
Public Function RShiftWord(ByVal w As Integer, ByVal c As Integer) As Integer
Dim dw As Long
If c = 0 Then
RShiftWord = w
Else
dw = w And &HFFFF&
dw = dw / (2 ^ c)
RShiftWord = dw And &HFFFF&
End If
End Function
'两个byte转换为一个integer
Public Function MakeWord(ByVal bHi As Byte, ByVal bLo As Byte) As Integer
If bHi And &H80 Then
MakeWord = (((bHi And &H7F) * 256) + bLo) Or &H8000&
Else
MakeWord = (bHi * 256) + bLo
End If
End Function
'两个integer转换为一个long
Public Function MakeDWord(wHi As Integer, wLo As Integer) As Long
If wHi And &H8000& Then
MakeDWord = (((wHi And &H7FFF&) * 65536) Or _
(wLo And &HFFFF&)) Or &H80000000
Else
MakeDWord = (wHi * 65535) + wLo
End If
End Function