繁体   English   中英

如何读取字节数组中的前 3 个字节

[英]how to read the first 3 bytes in byte array

我有字节数组,我只需要读取前 3 个字节而不是更多。

C# 4.0

这些足够了吗?

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

怎么样:

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

或者在一个数组中:

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

或者:

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

简单的 for 循环也可以完成这项工作。

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

或者只是在数组中使用索引。

byte first = byteArr[0];
byte second = byteArr[1];
byte third = byteArr[2];
byte b1 = bytearray[0];
byte b2 = bytearray[1];
byte b3 = bytearray[2];

数组从 0 开始索引,因此前 3 个字节位于数组的 0、1 和 2 槽中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM