How would I go about removing a number of bytes from a byte array?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT: As nobugz’s comment (and Reed Copsey’s answer) mentions, if you don’t actually need the result as a byte array, you should look into using
ArraySegment<T>:Otherwise, copying will be necessary – arrays always have a fixed size, so you can’t “remove” the first 16 bytes from the existing array. Instead, you’ll have to create a new, smaller array and copy the relevant data into it.
Zach’s suggestion is along the right lines for the non-LINQ approach, but it can be made simpler (this assumes you already know the original array is at least 16 bytes long):
or
I suspect
Buffer.BlockCopywill be slightly faster, but I don’t know for sure.Note that both of these could be significantly more efficient than the LINQ approach if the arrays involved are big: the LINQ approach requires each byte to be individually returned from an iterator, and potentially intermediate copies to be made (in the same way as adding items to a
List<T>needs to grow the backing array periodically). Obviously don’t micro-optimise, but it’s worth checking if this bit of code is a performance bottleneck.EDIT: I ran a very “quick and dirty” benchmark of the three approaches. I don’t trust the benchmark to distinguish between
Buffer.BlockCopyandArray.Copy– they were pretty close – but the LINQ approach was over 100 times slower.On my laptop, using byte arrays of 10,000 elements, it took nearly 10 seconds to perform 40,000 copies using LINQ; the above approaches took about 80ms to do the same amount of work. I upped the iteration count to 4,000,000 and it still only took about 7 seconds. Obviously the normal caveats around micro-benchmarks apply, but this is a pretty significat difference.
Definitely use the above approach if this is in a code path which is important to performance 🙂