>other than writing a loop
I dont think there is any built in support so you would have to use loops.
Main Topics
Browse All TopicsHi,
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
"I am converting random sized strings into byte arrays."
Perhaps it would be a lot easier to work with the original STRINGS first and then convert them to byte arrays?
You could easily "repeat fill" a string with a simple function:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = "abcde"
Debug.Print("str = " & str)
str = RepeatFill(str, 8)
Debug.Print("str = " & str)
End Sub
Private Function RepeatFill(ByVal S As String, ByVal Length As Integer) As String
' make sure it is long enough...
While S.Length < Length
S = S & S
End While
Return S.Substring(0, Length) ' ...then truncate it
End Function
Now your STRING is the desired length and you can just convert it directly to bytes with your existing method.
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
Business Accounts
Answer for Membership
by: jpaulinoPosted on 2009-07-14 at 06:55:42ID: 24849494
Hi,
For that you need to check the the size is smaller and then loop on the second array and fill with information from the first.