Solved

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

Posted on 2009-07-14
5
921 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
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 85

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now