How to remove bytes from a byte array

I have a program with a byte array, the size of which depends on the size, but is about 2300 bytes. I want to create a function that will create a new byte array by deleting all the bytes that I pass to it. For instance:

byte[] NewArray = RemoveBytes(OldArray,0xFF); 

I need a function that will remove any bytes equal to 0xFF and return me a new byte array.

Any help would be greatly appreciated. By the way, I am using C #.

+4
source share
2 answers

You can use the Where extension method to filter the array:

 byte[] newArray = oldArray.Where(b => b != 0xff).ToArray(); 

or if you want to delete multiple items, you can use Except for the extension method :

 byte[] newArray = oldArray.Except(new byte[] { 0xff, 0xaa }).ToArray(); 
+12
source

Abort the problem:

Can you write code to copy from one array to another?

Can you write a conditional expression?

Is it possible to allocate a new array?

If you are stuck on one of them, ask about it specifically.

-1
source

All Articles