Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:58:52+00:00 2026-05-14T02:58:52+00:00

How would I go about removing a number of bytes from a byte array?

  • 0

How would I go about removing a number of bytes from a byte array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:58:52+00:00Added an answer on May 14, 2026 at 2:58 am

    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>:

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

    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):

    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 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.BlockCopy and Array.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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about removing the & symbol from a string. It's making
How would I go about removing the / from the end of a URL
How would I go about removing numbers and a space from the start of
How would I go about removing numbers and a space from the start of
Let's say I have a string: http://www.foo.com/#bar How would I go about removing everything
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
Any ideas how I would go about writing a javascript method to insert an
Just a quick question about how you would go about implementing this. I want
I would like to know how I would go about altering the HTML that
I'm wondering how you would go about designing a good permgen space string in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.