I have byte array and I need to read only the first 3 bytes not more.
C# 4.0
Yes, by definition the size of a variable of type byte is one byte. So the length of your array is indeed array.
We can also get the byte array using the below code. byte[] byteArr = str. getBytes("UTF-8");
A byte is 8 bits (binary data). A byte array is an array of bytes (tautology FTW!). You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.
The bytearray() method returns a bytearray object, which is an array of the given bytes. The bytearray class is a mutable sequence of integers in the range of 0 to 256.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With