Excel CopyMemory 4 bytes to Integer
I am trying to copy four bytes into an integer variable. In short, I wants to perform UNION in vba. My function:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
and
Function Byte2Int32(ByVal Num1 As Byte, ByVal Num2 As Byte, ByVal Num3 As Byte, ByVal Num4 As Byte) As Integer
Dim D As Integer
Dim TabData(3) As Long
TabData(0) = Num4
TabData(1) = Num3
TabData(2) = Num2
TabData(3) = Num1
CopyMemory D, TabData(0), 4
Byte2Int32 = D
End Function
Function call: =Byte2Int32(C6;D6;E6;F6)
where C6 = 40, D6 = 20, E6 = 50, F6 = 68.
Results: 68. What should I change so that I can copy the memory? Ultimately, the function is supposed to work for the variable double ?. For two bytes, it works.
Some types on your function are incorrect. This version seems to be working:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Function Byte2Int32(ByVal Num1 As Byte, ByVal Num2 As Byte, ByVal Num3 As Byte, ByVal Num4 As Byte) As Long
Dim D As Long
Dim TabData(3) As Byte
TabData(0) = Num4
TabData(1) = Num3
TabData(2) = Num2
TabData(3) = Num1
CopyMemory D, TabData(0), 4
Byte2Int32 = D
End Function