Concat byte arrays

Concat byte arrays

//create two byte arrays with the following values
byte [] array1 = new byte [3] { 0, 1, 2 };
byte [] array2 = new byte [3] { 4, 5, 6 };

//create a byte array named concat whose length should be the sum of the above two arrays which has to be concatenated.
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, array1.Length, array2.Length);

//The System.Buffer.BlockCopy takes the following arguements
(source array, starting position of the source array, destination array, starting position of the destination array, length of the source array)

//Thus the concat byte array contains the following values {0,1,2,4,5,6} after concatenation.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.