473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Concatenate Byte[] arrays

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

Thanks.
Nov 16 '05 #1
4 73936
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" <ma**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a Byte[] array and I want to concatenate it to another Byte[] array.
How can I do this?

Thanks.

Nov 16 '05 #2
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" <de****@oslokb.no> wrote in message
news:KY********************@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" <ma**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a Byte[] array and I want to concatenate it to another Byte[]
array.
How can I do this?

Thanks.


Nov 16 '05 #3
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]
Nov 16 '05 #4
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" <de****@oslokb.no> wrote in message
news:l%********************@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" <de****@oslokb.no> wrote in message
news:KY********************@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" <ma**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a Byte[] array and I want to concatenate it to another Byte[]
array.
How can I do this?

Thanks.



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
7
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
8
by: frekster | last post by:
Hi. I used to be able to do this easily in vb 6 via looping and preserving the source array data/size etc. How can I do this in vb.net? I've been trying for a while now and this should be...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
5
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
0
by: JonJacobs | last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will...
3
by: RobbSadler | last post by:
This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed. I used his code from that post to create this, and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.