Best way to combine two or more byte arrays in C#
For primitive types (including bytes), use System.Buffer.BlockCopy
instead of System.Array.Copy
. It's faster.
I timed each of the suggested methods in a loop executed 1 million times using 3 arrays of 10 bytes each. Here are the results:
- New Byte Array using
System.Array.Copy
- 0.2187556 seconds - New Byte Array using
System.Buffer.BlockCopy
- 0.1406286 seconds - IEnumerable<byte> using C# yield operator - 0.0781270 seconds
- IEnumerable<byte> using LINQ's Concat<> - 0.0781270 seconds
I increased the size of each array to 100 elements and re-ran the test:
- New Byte Array using
System.Array.Copy
- 0.2812554 seconds - New Byte Array using
System.Buffer.BlockCopy
- 0.2500048 seconds - IEnumerable<byte> using C# yield operator - 0.0625012 seconds
- IEnumerable<byte> using LINQ's Concat<> - 0.0781265 seconds
I increased the size of each array to 1000 elements and re-ran the test:
- New Byte Array using
System.Array.Copy
- 1.0781457 seconds - New Byte Array using
System.Buffer.BlockCopy
- 1.0156445 seconds - IEnumerable<byte> using C# yield operator - 0.0625012 seconds
- IEnumerable<byte> using LINQ's Concat<> - 0.0781265 seconds
Finally, I increased the size of each array to 1 million elements and re-ran the test, executing each loop only 4000 times:
- New Byte Array using
System.Array.Copy
- 13.4533833 seconds - New Byte Array using
System.Buffer.BlockCopy
- 13.1096267 seconds - IEnumerable<byte> using C# yield operator - 0 seconds
- IEnumerable<byte> using LINQ's Concat<> - 0 seconds
So, if you need a new byte array, use
byte[] rv = new byte[a1.Length + a2.Length + a3.Length];
System.Buffer.BlockCopy(a1, 0, rv, 0, a1.Length);
System.Buffer.BlockCopy(a2, 0, rv, a1.Length, a2.Length);
System.Buffer.BlockCopy(a3, 0, rv, a1.Length + a2.Length, a3.Length);
But, if you can use an IEnumerable<byte>
, DEFINITELY prefer LINQ's Concat<> method. It's only slightly slower than the C# yield operator, but is more concise and more elegant.
IEnumerable<byte> rv = a1.Concat(a2).Concat(a3);
If you have an arbitrary number of arrays and are using .NET 3.5, you can make the System.Buffer.BlockCopy
solution more generic like this:
private byte[] Combine(params byte[][] arrays)
{
byte[] rv = new byte[arrays.Sum(a => a.Length)];
int offset = 0;
foreach (byte[] array in arrays) {
System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
offset += array.Length;
}
return rv;
}
*Note: The above block requires you adding the following namespace at the the top for it to work.
using System.Linq;
To Jon Skeet's point regarding iteration of the subsequent data structures (byte array vs. IEnumerable<byte>), I re-ran the last timing test (1 million elements, 4000 iterations), adding a loop that iterates over the full array with each pass:
- New Byte Array using
System.Array.Copy
- 78.20550510 seconds - New Byte Array using
System.Buffer.BlockCopy
- 77.89261900 seconds - IEnumerable<byte> using C# yield operator - 551.7150161 seconds
- IEnumerable<byte> using LINQ's Concat<> - 448.1804799 seconds
The point is, it is VERY important to understand the efficiency of both the creation and the usage of the resulting data structure. Simply focusing on the efficiency of the creation may overlook the inefficiency associated with the usage. Kudos, Jon.
Append two or more byte arrays in C#
You want BlockCopy
According to this blog post it is faster than Array.CopyTo.
c# Combine / Decombine Two byte Arrays
First,
Array.Copy(array, f, array.Length - (array.Length - first));
is equivalent to
Array.Copy(array, f, first);
Second,
Array.Copy(array, s, array.Length - first);
starts copying array
from the 0 index. What you want is
Array.Copy(array,first, s, 0, array.Length - first);
Merging two arrays in .NET
If you can manipulate one of the arrays, you can resize it before performing the copy:
T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
int array1OriginalLength = array1.Length;
Array.Resize<T>(ref array1, array1OriginalLength + array2.Length);
Array.Copy(array2, 0, array1, array1OriginalLength, array2.Length);
Otherwise, you can make a new array
T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
T[] newArray = new T[array1.Length + array2.Length];
Array.Copy(array1, newArray, array1.Length);
Array.Copy(array2, 0, newArray, array1.Length, array2.Length);
More on available Array methods on MSDN.
Combine two mutually exclusive byte arrays into one
To combine bits in one byte with bits in another byte, you can use the bitwise-or operator |
. This operator will set a bit in the resulting byte if that bit was set in first or second byte.
Example:
byte b1 = 0x12; // 0001 0010
byte b2 = 0x81; // 1000 0001
byte result = (byte)(b1 | b2); // Results in 0x93 = 1001 0011
To combine the two arrays:
byte[] flags1 = ...;
byte[] flags2 = ...;
byte[] result = new byte[5];
for(int i = 0; i < 5; i++)
result[i] = (byte)(flags[i] | flags[2]);
Using the bitwise-AND operator &
you can find out if any bit is set in both bytes. Example:
byte b1 = 0x93; // 1001 0011
byte b2 = 0x1A; // 0001 1010
byte result = (byte)(b1 & b2); // Results in 0x12 = 0001 0010
To check if bits are NOT set in both arrays:
byte[] flags1 = ...;
byte[] flags2 = ...;
for(int i = 0; i < 5; i++)
if ((byte)(flags[i] & flags[2]) != 0)
throw new InvalidOperationException("Flags cannot be set in both arrays.");
Related Topics
Is There Really Any Way to Uniquely Identify Any Computer at All
C# - Winforms - Global Variables
Compare Two Datatables to Determine Rows in One But Not the Other
An Attribute Argument Must Be a Constant Expression, ...- Create an Attribute of Type Array
Main Method Code Entirely Inside Try/Catch: Is It Bad Practice
How to Bind List to Datagridview
Convert Byte Array to Image in Wpf
Multiple String Comparison with C#
C# Rsa Public Key Output Not Correct
How to Implement Url Rewriting Similar to So
What Is the Connection String for Localdb for Version 11
Ef Code-First One-To-One Relationship: Multiplicity Is Not Valid in Role * in Relationship
How to Convert a Utf-8 String into Unicode
C# Reflection - Get Field Values from a Simple Class
Formatting Text in a Textblock
Why Do I Get an Outofmemoryexception When I Have Images in My Listbox