Delete the first 16 bytes?

How can I remove multiple bytes from a byte array?

+6
c # bytearray
source share
4 answers

EDIT: As nobugz's comment (and Reed Copsey's answer) mentions, if you really don't need the result as a byte array, you should examine ArraySegment<T> :

 ArraySegment<byte> segment = new ArraySegment<byte>(full, 16, full.Length - 16); 

Otherwise, copying is required - arrays always have a fixed size, so you cannot "delete" the first 16 bytes from an existing array. Instead, you will need to create a new smaller array and copy the corresponding data into it.

The Zach clause is along the right lines for a non-LINQ approach, but it can be simplified (assuming you already know that the original array is at least 16 bytes long):

 byte[] newArray = new byte[oldArray.Length - 16]; Buffer.BlockCopy(oldArray, 16, newArray, 0, newArray.Length); 

or

 byte[] newArray = new byte[oldArray.Length - 16]; Array.Copy(oldArray, 16, newArray, 0, newArray.Length); 

I suspect Buffer.BlockCopy will be a little faster, but I don’t know for sure.

Note that both of them can be significantly more efficient than the LINQ approach if the arrays involved are large: the LINQ approach requires each byte to be individually returned from the iterator, and potentially intermediate copies must be made (in the same way as adding elements in List<T> should periodically increase the backup array). Obviously, this is not micro-optimization, but it's worth checking if this bit of code is a performance bottleneck.

EDIT: I did a very β€œquick and dirty” test of three approaches. I don't trust the benchmark to distinguish between Buffer.BlockCopy and Array.Copy - they were pretty close, but the LINQ approach was more than 100 times slower.

On my laptop, using byte arrays of 10,000 elements, it took almost 10 seconds to complete 40,000 copies using LINQ; the above approaches took about 80 ms to do the same job. I increased the number of iterations to 4,000,000, and it lasted only about 7 seconds. Obviously, the normal reservations around micro-benchmarks apply, but this is a pretty significant difference.

Definitely use the above approach if it is in code that is important for performance :)

+24
source share

You can do it:

 using System.Linq // ... var newArray = oldArray.Skip(numBytes).ToArray(); 
+14
source share

I also mentioned - depending on how you plan to use the results, often an alternative approach is to use ArraySegment<T> to simply access the rest of the array. This prevents the need to copy an array, which may be more efficient in some usage scenarios:

 ArraySegment<byte> segment = new ArraySegment<byte>(originalArray, 16, originalArray.Length-16); // Use segment how you'd use your array... 
+6
source share

If you cannot use Linq, you can do it like this:

 byte[] myArray = // however you acquire the array byte[] newArray = new byte[myArray.Length - 16]; for (int i = 0; i < newArray.Length; i++) { newArray[i] = myArray[i + 16]; } // newArray is now myArray minus the first 16 bytes 

You will also need to handle the case when the size of the array is less than 16 bytes.

0
source share

All Articles