Results 1 to 6 of 6

Thread: [RESOLVED] Mod array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    97

    Resolved [RESOLVED] Mod array

    hello i want to add elements in a byte array at the Begin of an array confuse any help

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Mod array

    try this:

    Code:
    Dim b1() As Byte = {&H4, &H5, &H6}
    Dim b2() As Byte = {&H1, &H2, &H3}
    Dim b3() As Byte = b2.Concat(b1).ToArray

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Mod array

    You can't. You'll need to create a new array combining an array of the new elements and the original array, so ...

    Dim CombinedArray() As Byte = NewElementsArray.Concat(OriginalByteArray)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Mod array

    You will need to create a new array, as noted, and there are a variety of ways to do that. The older way, making use of Array.Copy, is not quite as easy to read as what dunfiddlin showed, but I tested it out a while back, and Array.Copy is considerably faster than the alternatives. I think it uses some low level memory management code to perform a fast copy under the hood, though I'm not sure about that.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    97

    Re: Mod array

    Thanks everyone

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Mod array

    Quote Originally Posted by Shaggy Hiker View Post
    You will need to create a new array, as noted, and there are a variety of ways to do that. The older way, making use of Array.Copy, is not quite as easy to read as what dunfiddlin showed, but I tested it out a while back, and Array.Copy is considerably faster than the alternatives. I think it uses some low level memory management code to perform a fast copy under the hood, though I'm not sure about that.
    I'd say it is faster! So much so that if this code was intended for a high use area of the application I would NOT use concat.

    Code:
            Const alen As Integer = 99999
            Dim a1(alen) As Byte
            Dim a2(alen) As Byte
    
            'some data
            'For x As Integer = 1 To a1.Length - 1
            '    a1(x) = CByte(x And &HFE) 'note mask
            'Next
            a1(0) = &HFF 'set start of this array
            Array.Copy(a1, a2, a2.Length) 'copy a1 to a2
            a2(1) = &HFF 'a2 starts with FF FF
    
            Dim stpw As New Stopwatch
            Const tries As Integer = 10
            Dim c12() As Byte = New Byte() {}
            Dim li As Integer
    
            'use array copies to get combined array
            stpw.Restart()
            For x As Integer = 1 To tries
                Array.Resize(c12, 0) 'set size of destination
                Array.Resize(c12, a1.Length + a2.Length) 'set size of destination
                Array.Copy(a1, c12, a1.Length) 'copy a1 to combined
                Array.Copy(a2, 0, c12, a1.Length, a2.Length) 'copy a2 to combined
            Next
            stpw.Stop()
            Debug.WriteLine(stpw.Elapsed.TotalMilliseconds.ToString("n3"))
    
            li = Array.LastIndexOf(c12, CByte(&HFF)) 'show ffff
            Debug.WriteLine("{0}{1}  {2}", Convert.ToString(c12(li - 1), 16), Convert.ToString(c12(li), 16), li.ToString("n0"))
            Debug.WriteLine("")
    
            Array.Resize(c12, 0) 'set size of destination
            stpw.Restart()
            For x As Integer = 1 To tries
                c12 = a1.Concat(a2).ToArray '<<<<<<<<<<<< using concat
            Next
            stpw.Stop()
            Debug.WriteLine(stpw.Elapsed.TotalMilliseconds.ToString("n3"))
    
            li = Array.LastIndexOf(c12, CByte(&HFF)) 'show ffff
            Debug.WriteLine("{0}{1}  {2}", Convert.ToString(c12(li - 1), 16), Convert.ToString(c12(li), 16), li.ToString("n0"))
            Debug.WriteLine("")
    edit: I created a function to combine arrays using Array.Copy, and using it the code was still much faster(50x) than concat.
    Last edited by dbasnett; Dec 8th, 2012 at 11:38 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width