Link to home
Start Free TrialLog in
Avatar of tylerd
tylerd

asked on

combining arrays

is there a quick and effecient way of combining 2 dynamic byte arrays? Im willing to try any solution however.
thanks
Avatar of Vbmaster
Vbmaster

The fastest way to do this is using the CopyMem API, here's a example to add a Byte Array at the end of another Byte Array.

You first need to declare the API in the Declarations part

  Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

And here's the example source code

  Dim a() As Byte
  Dim b() As Byte
  Dim c As Long
 
  ReDim a(5)
  ReDim b(3)
 
  For c = 1 To 6
    a(c - 1) = c
  Next
  For c = 1 To 4
    b(c - 1) = c * 10
  Next
 
  ReDim Preserve a(5 + 4)
  Call CopyMem(a(6), b(0), 4)
  For c = 0 To UBound(a)
    List1.AddItem a(c)
  Next
 

Some background:

  The API CopyMem takes three parameters. The third parameter is a count of bytes, in the example I want to copy all the items in the Byte Array b, which is 4 bytes. The second parameter is what item to start with. Here I want to start with b(0), then it copies all from b(0) and 4 bytes from there. This will copy all the b array. The first parameter is where to copy it to. I want to add it to the where the a array ended, and that's item with index 6.

  Don't forget to ReDim Preserve the array to where you want to copy, if you don't you will copy over something else, destroying the data that is already there.

  Remember to always double-check the parameters, if you use it wrong you WILL destroy data in the memory and get GPF's but if this API is correctly used it is to enourmous help.


Hope this helps.
Avatar of tylerd

ASKER

thanks alot! just answer the question now and i'll give you the points
ASKER CERTIFIED SOLUTION
Avatar of Vbmaster
Vbmaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial