Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove bytes from a byte array

Tags:

c#

.net

bytearray

I have a program that has a byte array that varies in size, but is around 2300 bytes. What I want to do is create a function that will create a new byte array, removing all bytes that I pass to it. For example:

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 appreciated. I am using C#, by the way.

like image 712
Icemanind Avatar asked Oct 17 '10 16:10

Icemanind


1 Answers

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

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

or if you want to remove multiple elements you could use the Except extension method:

byte[] newArray = oldArray.Except(new byte[] { 0xff, 0xaa }).ToArray();
like image 179
Darin Dimitrov Avatar answered Sep 19 '22 23:09

Darin Dimitrov