[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-07-14
5
Medium Priority
?
1,000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

656 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