?
Solved

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

Posted on 2009-07-14
5
Medium Priority
?
994 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 400 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 1200 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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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