Link to home
Start Free TrialLog in
Avatar of mms_master
mms_masterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Copy Array - Repeat smaller array into a larger array until full

Hi,

I am converting random sized strings into byte arrays. I need the byte array to be a certain length. If the array is too long I can use this code:

Dim NewArray(CorrectLength) as Byte
Array.Copy(SourceArray, NewArray, NewArray.Length)

However if the array is too short I get an error "Source array was not long enough. Check srcIndex and length, and the array's lower bounds.". Is there a way (other than writing a loop) to copy the source array repeatidly until it fills the destination array. E.g. (Using strings instead of bytes to make it easier to understand)

Source(0) = "A"
Source(1) = "B"
Source(2) = "C"
Source(3) = "D"
Source(4) = "E"

'''Which would become:
'''Destination.Length = 8
Destination(0) = "A"
Destination(1) = "B"
Destination(2) = "C"
Destination(3) = "D"
Destination(4) = "E"
Destination(5) = "A"
Destination(6) = "B"
Destination(7) = "C"

Thanks in advance,
mms_master
SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

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
ASKER CERTIFIED SOLUTION
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
SOLUTION
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
Avatar of mms_master

ASKER

Thank you all for your replies.

@CodeCruiser
Thank you, thatsall I  wanted to know.

@Idle_Mind
Thank you for the suggestion. But as I have allready written a loop for the arrays, I will stick with that. I was just curious to find out if there was an existing function for it.

@jpaulino
Thanks. This is the method I am currently using. I was just wondering if there was built in support.

I am going to give the majority of the points to CodeCruiser as he gave the answer I was looking for. However Im going to give a few points to ipaulino and Idle_Mind for their suggestions.

Thank you all again,
mms_master
Thank you.