|
-
Aug 7th, 2009, 08:56 AM
#1
Thread Starter
Addicted Member
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)
-
Aug 7th, 2009, 09:48 AM
#2
Fanatic Member
Re: Combining 2 Byte Arrays
Hey rkeslar, I was wondering the same thing last night. Your code should look like this:
vb.net Code:
Dim bytearray3() As Byte = bytearray1.Concat(bytearray2).ToArray()
-
Aug 7th, 2009, 09:57 AM
#3
Thread Starter
Addicted Member
Re: Combining 2 Byte Arrays
 Originally Posted by tassa
Hey rkeslar, I was wondering the same thing last night. Your code should look like this:
vb.net Code:
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?
-
Aug 7th, 2009, 10:01 AM
#4
Re: Combining 2 Byte Arrays
Concat is actually an extension only useable in .NET 3.5 and up
-
Aug 7th, 2009, 10:02 AM
#5
Thread Starter
Addicted Member
Re: Combining 2 Byte Arrays
Oh that sucks. Wish I was working in 3.5 now.
-
Aug 7th, 2009, 10:16 AM
#6
Re: Combining 2 Byte Arrays
This is probably more or less what Concat does anyway:
vb.net Code:
Public Shared Function Concat(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource) If first Is Nothing Then Throw New NullReferenceException("Parameter 'first' cannot be null.") If second Is Nothing Then Throw New NullReferenceException("Parameter 'second' cannot be null.") Dim concatenation As New List(Of TSource)(first) concatenation.AddRange(second) Return CType(concatenation, IEnumerable(Of TSource)) End Function
-
Aug 7th, 2009, 10:24 AM
#7
Thread Starter
Addicted Member
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?
-
Aug 7th, 2009, 10:38 AM
#8
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:
Public Sub SomeMethod() Dim firstArray() As String = {"first", "array", "values"} Dim secondArray() As String = {"second", "array", "values", "also"} Dim concatenation() As String = ToArray(Concat(firstArray, secondArray)) For Each element As String In concatenation MessageBox.Show(element) Next End Sub Public Shared Function Concat(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource) If first Is Nothing Then Throw New NullReferenceException("Parameter 'first' cannot be null.") If second Is Nothing Then Throw New NullReferenceException("Parameter 'second' cannot be null.") Dim concatenation As New List(Of TSource)(first) concatenation.AddRange(second) Return CType(concatenation, IEnumerable(Of TSource)) End Function Public Shared Function ToArray(Of TSource)(ByVal source As IEnumerable(Of TSource)) As TSource() If source Is Nothing Then Throw New NullReferenceException("Parameter 'source' cannot be null.") Dim listTSource As New List(Of TSource)(source) Return listTSource.ToArray() 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).
-
Aug 7th, 2009, 01:16 PM
#9
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 -
-
Aug 7th, 2009, 01:27 PM
#10
Fanatic Member
Re: Combining 2 Byte Arrays
You can do this:
vb.net Code:
Dim _byte As Byte() = {1, 2, 3} Dim _2byte As Byte() = {4, 5, 6} Dim length As Integer = _byte.Length + _2byte.Length '_newByte is a new byte array that will hold _ 'the concatenation of _byte and _2byte Dim _newByte As Byte() = New Byte(length) {} _byte.CopyTo(_newByte, 0) _2byte.CopyTo(_newByte, _byte.Length)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|