0

I have a byte array . I need to remove the bytes at specific index and split the byte arrays. For example Say I have a byte array of length 1000 . And I need to remove the bytes from position 50 to 200 .So my expected result would be 2 byte arrays . One is 0-49 and another is 201-1000.

Is Array.RemoveAt the only way to remove the byte array with index?

Thanks in advance!

3
  • No. Create new arrays and write bytes from full one. RemoveAt removes specified byte from array - you wont get 2 separate arrays. Commented Dec 2, 2014 at 12:09
  • 1
    How performant does this have to be? If speed isn't critical, the Linq solution someone posted will be fine. If speed is critical, use Array.Copy to copy the subsections into two new arrays. (But even then, you should perform careful timings to see if that's really necessary!) Commented Dec 2, 2014 at 12:26
  • @MatthewWatson: speed is critical. the byte array contains a lot of data and the bytes that has to be removed can also be a large. Commented Dec 2, 2014 at 12:30

4 Answers 4

3

If speed is critical, you can create two new arrays and use Array.Copy() to copy the required bytes into them.

To make this easier to use, it might be more convenient to write a little extension method for arrays which extracts and returns a subset, like this (note: error handling omitted for brevity):

public static class ArrayExt
{
    public static T[] Subset<T>(this T[] array, int start, int count)
    {
        T[] result = new T[count];
        Array.Copy(array, start, result, 0, count);
        return result;
    }
}

Then you can use it like so (I have corrected the index from your example; you had it starting at 201, but it should have been 200):

var array1 = new byte[1000];

// ... populate array1 somehow, then extract subsets like so:

var array2 = array1.Subset(  0,  50);
var array3 = array1.Subset(200, 800);

// Now array2 and array3 are two byte arrays 
// containing the required bytes.

This is probably the fastest you are likely to get.

Sign up to request clarification or add additional context in comments.

Comments

2

You could use IEnumerable Take and Skip, for example

byte[] origin = new byte[200];
for(int i = 0; i < origin.Length; i++)
    origin[i] = (byte)i;

byte[] first = origin.Take(50).ToArray();
byte[] second = origin.Skip(100).Take(50).ToArray();

Comments

1

Below code can be used to split the byte array, "index" starting index and "count" is the index count upto which index you want to split, here for your condition index=50 and count is 150;

List<byte> byteArray = array.ToList(); //array is the byte array
byteArray.RemoveRange(index, count);
Byte[] array1 = byteArray.ToArray(); //array1 is the resultant byte array

Comments

0

Cant you get the sub arrays like this?

byte[] array1 = array.ToList().GetRange(0, x1-1).ToArray();
byte[] array2 = array.ToList().GetRange(x2, array.Length- x2 - 1).ToArray();

where x1 and x2 is 50 and 200 in your example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.