Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a range of bytes from a bytearray in c#

Tags:

c#

I have to read a range of bytes from a byte array. I have the starting position and the ending position to read.

-(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 the range of data(bytes) from a NSData(byteArray). Is there any equivelent method in c# to do the same. or how else we can do this. Please advise!

like image 354
saikamesh Avatar asked Mar 25 '11 06:03

saikamesh


People also ask

What is the difference between bytes and Bytearray?

The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.

How do you find the byte of a string?

Java String getBytes() The Java String getBytes() method encodes the string into a sequence of bytes and stores it in a byte array. Here, string is an object of the String class. The getBytes() method returns a byte array.

How do you reverse Bytearray?

you can use the linq method: MyBytes. Reverse() as well as the Array. Reverse() method.


1 Answers

What do you mean by read? Copy a range of bytes into 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

like image 163
carlsb3rg Avatar answered Oct 29 '22 15:10

carlsb3rg