Solved

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

Posted on 2009-07-14
5
980 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:mms_master
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 100 total points
ID: 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.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 300 total points
ID: 24850220
>other than writing a loop
I dont think there is any built in support so you would have to use loops.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 24851787
"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.
0
 
LVL 5

Author Comment

by:mms_master
ID: 24852685
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
0
 
LVL 5

Author Closing Comment

by:mms_master
ID: 31603220
Thank you.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question