Concatenate Byte[] arrays

  • Thread starter Thread starter Marius Cabas
  • Start date Start date
M

Marius Cabas

I have a Byte[] array and I want to concatenate it to another Byte[] array.
How can I do this?

Thanks.
 
You should create a new array and copy the data there:
byte [] array1 = new byte [3] { 0, 1, 2 };
byte [] array2 = new byte [3] { 4, 5, 6 };
byte [] concat = new byte [array1.Length + array2.Length];
Then, preferably use System.Buffer.BlockCopy to copy the data, since it is a
lot faster than System.Array.Copy:
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, 0, array2.Length);
 
Sorry;
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, 0, array2.Length);
should be:
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, array1.Length - 1, array2.Length);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
You should create a new array and copy the data there:
byte [] array1 = new byte [3] { 0, 1, 2 };
byte [] array2 = new byte [3] { 4, 5, 6 };
byte [] concat = new byte [array1.Length + array2.Length];
Then, preferably use System.Buffer.BlockCopy to copy the data, since it is
a lot faster than System.Array.Copy:
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, 0, array2.Length);




--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Marius Cabas said:
I have a Byte[] array and I want to concatenate it to another Byte[]
array.
How can I do this?

Thanks.
 
Hi Marius,

You can create a new array with the size of the total of the two arrays.
Use the overloaded Array.Copy with detailed index positions to copy the two.

An alternate method would be to add the two arrays to an ArrayList using
AddRange and then retrieve the finished array with ToArray.

Happy Coding!
Morten Wennevik [C# MVP]
 
My god, i need this weekend coming up:
Sorry;
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, 0, array2.Length);
should be:
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, array1.Length, array2.Length);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Sorry;
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, 0, array2.Length);
should be:
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, array1.Length - 1, array2.Length);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
You should create a new array and copy the data there:
byte [] array1 = new byte [3] { 0, 1, 2 };
byte [] array2 = new byte [3] { 4, 5, 6 };
byte [] concat = new byte [array1.Length + array2.Length];
Then, preferably use System.Buffer.BlockCopy to copy the data, since it
is a lot faster than System.Array.Copy:
System.Buffer.BlockCopy
(array1, 0, concat, 0, array1.Length);
System.Buffer.BlockCopy
(array2, 0, concat, 0, array2.Length);




--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Marius Cabas said:
I have a Byte[] array and I want to concatenate it to another Byte[]
array.
How can I do this?

Thanks.
 
Back
Top