Results 1 to 10 of 10

Thread: Combining 2 Byte Arrays

  1. #1

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Question Combining 2 Byte Arrays

    Hello,

    I'm trying to combine 2 byte arrays and then create a new byte array that contains the contents of both but running into a problem. The error says 'Source Array was not long enough.' I'm trying to combine HeaderByteArray and bytBuffer into newByteArray. I don't understand what Source Array is not long enough means and how to correct this. Could anyone offer some guidance? Thanks


    Here is my code and the values for each line when running through the debugger:

    Code:
    ' 4343
    Dim newArrayLength As Integer = (bytHeader.Length + bytBuffer.Length) - 1
    
    Dim newByteArray(newArrayLength) As Byte
    
    ' bytHeader: Length=248
    ' newByteArray: Length=4344
    ' bytHeader.Length: 248
    Array.Copy(bytHeader, newByteArray, bytHeader.Length)
    
    
    ' bytBuffer: Length=4096
    ' newByteArray: Length=4344
    ' bytHeader.Length=248
    ' newByteArray.Length=4344
    Array.Copy(bytBuffer, 0, newByteArray, bytHeader.Length, newByteArray.Length)

  2. #2
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Combining 2 Byte Arrays

    Hey rkeslar, I was wondering the same thing last night. Your code should look like this:

    vb.net Code:
    1. Dim bytearray3() As Byte = bytearray1.Concat(bytearray2).ToArray()
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  3. #3

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Question Re: Combining 2 Byte Arrays

    Quote Originally Posted by tassa View Post
    Hey rkeslar, I was wondering the same thing last night. Your code should look like this:

    vb.net Code:
    1. Dim bytearray3() As Byte = bytearray1.Concat(bytearray2).ToArray()

    How did you get Concat to come up in Intellisense? I get an error that says Concat is not a member of System.Array. Did you have to import a namespace?

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Combining 2 Byte Arrays

    Concat is actually an extension only useable in .NET 3.5 and up

  5. #5

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Re: Combining 2 Byte Arrays

    Oh that sucks. Wish I was working in 3.5 now.

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Combining 2 Byte Arrays

    This is probably more or less what Concat does anyway:

    vb.net Code:
    1. Public Shared Function Concat(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource)
    2.         If first Is Nothing Then Throw New NullReferenceException("Parameter 'first' cannot be null.")
    3.         If second Is Nothing Then Throw New NullReferenceException("Parameter 'second' cannot be null.")
    4.         Dim concatenation As New List(Of TSource)(first)
    5.         concatenation.AddRange(second)
    6.         Return CType(concatenation, IEnumerable(Of TSource))
    7.     End Function

  7. #7

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Re: Combining 2 Byte Arrays

    What is TSource? I get an error that says 'System.Collections.IEnumerable' has no type parameters and so cannot have type arguments.'

    Also, did you have to import a namespace to declare concatenation as a New List?

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Combining 2 Byte Arrays

    TSource is a generic. You will probably want to do some research on that to learn exactly what it entails. I feel like I'm rewriting LINQ:

    vb.net Code:
    1. Public Sub SomeMethod()
    2.         Dim firstArray() As String = {"first", "array", "values"}
    3.         Dim secondArray() As String = {"second", "array", "values", "also"}
    4.         Dim concatenation() As String = ToArray(Concat(firstArray, secondArray))
    5.         For Each element As String In concatenation
    6.             MessageBox.Show(element)
    7.         Next
    8.     End Sub
    9.  
    10.     Public Shared Function Concat(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource)
    11.         If first Is Nothing Then Throw New NullReferenceException("Parameter 'first' cannot be null.")
    12.         If second Is Nothing Then Throw New NullReferenceException("Parameter 'second' cannot be null.")
    13.         Dim concatenation As New List(Of TSource)(first)
    14.         concatenation.AddRange(second)
    15.         Return CType(concatenation, IEnumerable(Of TSource))
    16.     End Function
    17.  
    18.     Public Shared Function ToArray(Of TSource)(ByVal source As IEnumerable(Of TSource)) As TSource()
    19.         If source Is Nothing Then Throw New NullReferenceException("Parameter 'source' cannot be null.")
    20.         Dim listTSource As New List(Of TSource)(source)
    21.         Return listTSource.ToArray()
    22.     End Function

    I used strings in the example, but you can use anything you want. This is what a generic parameter (TSource) allows you to do. You can now use those Concat and ToArray functions for object that implements IEnumerable(Of T).

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Combining 2 Byte Arrays

    Why not just use a List(Of Byte), call AddRange to add the 2 byte arrays to it and then call ToArray to have the final output array? The same idea as FA has, but his code is way overkills.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Combining 2 Byte Arrays

    You can do this:

    vb.net Code:
    1. Dim _byte As Byte() = {1, 2, 3}
    2.         Dim _2byte As Byte() = {4, 5, 6}
    3.         Dim length As Integer = _byte.Length + _2byte.Length
    4.         '_newByte is a new byte array that will hold _
    5.         'the concatenation of _byte and _2byte
    6.         Dim _newByte As Byte() = New Byte(length) {}
    7.         _byte.CopyTo(_newByte, 0)
    8.         _2byte.CopyTo(_newByte, _byte.Length)
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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