开发者

how to read the first 3 bytes in byte array

I have byte array 开发者_运维技巧and I need to read only the first 3 bytes not more.

C# 4.0


Any of these enough?

IEnumerable<byte> firstThree = myArray.Take(3);
byte[] firstThreeAsArray = myArray.Take(3).ToArray();
List<byte> firstThreeAsList = myArray.Take(3).ToList();
byte[] firstThreeAsArraySlice = myArray[..3];


How about:

Byte byte1 = bytesInput[0];
Byte byte2 = bytesInput[1];
Byte byte3 = bytesInput[2];

Or in an array:

Byte[] threeBytes = new Byte[] { bytesInput[0], bytesInput[1], bytesInput[2] };

Or:

Byte[] threeBytes = new Byte[3];
Array.Copy(bytesInput, threeBytes, 0, 3); 
     // not sure on the overload but its similar to this


Simple for loop can also do the job.

for(int i = 0; i < 3; i++) 
{
   // your logic
}

Or just use index in array.

byte first = byteArr[0];
byte second = byteArr[1];
byte third = byteArr[2];


byte b1 = bytearray[0];
byte b2 = bytearray[1];
byte b3 = bytearray[2];

An array is indexed from 0, so the first 3 bytes are in the 0, 1 and 2 slots in your array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜