Tags: array, arrays, byte, concatenate, net, sharp

Concatenate Byte[] arrays

On .Net » .Net C# (C sharp)

4,779 words with 4 Comments; publish: Mon, 02 Jun 2008 19:30:00 GMT; (100296.88, « »)

I have a Byte[] array and I want to concatenate it to another Byte[] array.

How can I do this?

Thanks.

All Comments

Leave a comment...

  • 4 Comments
    • 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" <marius_cabas.net-csharp.todaysummary.com.hotmail.com> wrote in message

      news:%238AHQYKyEHA.2752.net-csharp.todaysummary.com.TK2MSFTNGP11.phx.gbl...

      >I have a Byte[] array and I want to concatenate it to another Byte[] array.

      > How can I do this?

      > Thanks.

      >

      #1; Mon, 02 Jun 2008 19:31:00 GMT
    • 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" <dennis.net-csharp.todaysummary.com.oslokb.no> wrote in message

      news:KY0ld.16284$Km6.175084.net-csharp.todaysummary.com.news4.e.nsc.no...

      > 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" <marius_cabas.net-csharp.todaysummary.com.hotmail.com> wrote in message

      > news:%238AHQYKyEHA.2752.net-csharp.todaysummary.com.TK2MSFTNGP11.phx.gbl...

      >>I have a Byte[] array and I want to concatenate it to another Byte[]

      >>array.

      >> How can I do this?

      >> Thanks.

      >>

      >

      #2; Mon, 02 Jun 2008 19:32:00 GMT
    • 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]

      #3; Mon, 02 Jun 2008 19:33:00 GMT
    • 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" <dennis.net-csharp.todaysummary.com.oslokb.no> wrote in message

      news:l%0ld.16285$Km6.174900.net-csharp.todaysummary.com.news4.e.nsc.no...

      > 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" <dennis.net-csharp.todaysummary.com.oslokb.no> wrote in message

      > news:KY0ld.16284$Km6.175084.net-csharp.todaysummary.com.news4.e.nsc.no...

      >> 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" <marius_cabas.net-csharp.todaysummary.com.hotmail.com> wrote in message

      >> news:%238AHQYKyEHA.2752.net-csharp.todaysummary.com.TK2MSFTNGP11.phx.gbl...

      >>I have a Byte[] array and I want to concatenate it to another Byte[]

      >>array.

      >> How can I do this?

      >> Thanks.

      >>

      >>

      >

      #4; Mon, 02 Jun 2008 19:34:00 GMT