How to read byte range from bytearray in c #

I need to read a range of bytes from an array of bytes. I have a starting position and an ending position for reading.

-(NSData *) getSubDataFrom:(int)stPos To:(int)endPos withData:(NSData *) data{ NSRange range = NSMakeRange(stPos, endPos); return [data subDataWithRage:range]; } 

The above code in ObjectiveC reads a range of data (bytes) from NSData (byteArray). Is there any equivalent method in C # to do the same. or how else can we do this. Please advise!

+4
source share
2 answers

What do you understand by reading? Copy byte range to another byte array?

 var mainArray = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var startPos = 5; var endPos = 10; var subset = new byte[endPos - startPos + 1]; Array.Copy(mainArray, startPos, subset, 0, endPos - startPos + 1); 

From MSDN

+6
source

Try the Array.Copy () or Array.CopyTo () method .

+1
source

Source: https://habr.com/ru/post/1345195/


All Articles