yose yose - 2 months ago 11
C# Question

C# how to read only selected value in byte array[]?

i have array byte like this in c# :

byte[] reply = new byte[] {09 02 08 63 01 01 10 21 03 5D 01 06};


how to only read { 01, 06 } value in byte array?
what must i do? i really newbie in c#. thanks for attention.

Answer

First off, I think you're declaring the array in the wrong way, you're missing the commas.

byte[] reply = new byte[] {09, 02, 08, 63, 01, 01, 10, 21, 03, 5D, 01, 06};

Next, if you know the 2 values you want to read: (add using System.Linq; to your namespaces first)

byte[] values = reply.Where(val => val == 01 || val == 06).ToArray();

Or, if you have the values you need in another array:

byte[] need = new byte[] { 01, 06 };
byte[] values = reply.Where(val => need.Contains(val)).ToArray();
Comments