2

I am a newbie with a question regarding reading specific bytes out of an array in C#. I received an array response that is 128 bytes long and I am looking to read and store the first 4 bytes of the array. How do I do so?

I've read a number of posts about shifting bytes but was a bit confused and I am looking to get going in the right direction.

3
  • 1
    What data type do you expect the bytes to represent? i.e. do you just want to process them as individual bytes, or are the first 4 bytes actually an integer? Commented Jan 23, 2014 at 15:21
  • Agreed, this is surely an XY question. Commented Jan 23, 2014 at 15:33
  • I want to process them as bytes and store them in an array so that it reads { 1,2,3,4} Commented Jan 23, 2014 at 15:43

5 Answers 5

5

Use one of the Array.Copy overloads:

byte[] bytes = new byte[4];

Array.Copy(originalArray, bytes, bytes.Length);
Sign up to request clarification or add additional context in comments.

Comments

2

Use Array.Copy method:

// your original array
var sourceArray = new byte[128];

// create a new one with the size you need
var firstBytes = new byte[4];

// copy the required number of bytes.
Array.Copy(sourceArray, 0, firstBytes, 0, firstBytes.Length);

If you do not need the rest of the data in the original array, you can also resize it, truncating everything after the first 4 bytes:

Array.Resize(ref sourceArray, 4);

Comments

1

Read the first 4 bytes in 4 different variables:

byte first = array[0],
    second = array[1],
    third = array[2],
    forth = array[4];

Read them as one 32-bit long integer:

int result = 
    first << 24 ||
    second << 16 ||
    third << 8 ||
    forth;

The x << n operator shifts the bits of number x, n times to the left. You are effectively moving the bytes of first 24 positions to the left, thus first will be stored in bits with indices 24 to 31 like that:

first 00000000 00000000 00000000

Next, move second 16 positions to the left, thus storing it in bits 16 to 23 and so on:

first second 00000000 00000000

Wikipedia provides a good summary of bitwise operations if you need more details.

1 Comment

BitConverter makes it possible to avoid the bit shifting. msdn.microsoft.com/en-us/library/bb384066.aspx
1

You could use the Take method. Although, this is only available if you are using .NET 3.5 or later. You will need to have using System.Linq; at the top of the .cs file.

var bytes = originalArray.Take(4).ToArray();

Comments

0

Example from a network stream.

byte[] bytesRead = 0;
using (NetworkStream stream = client.GetStream())
{
    byte[] length = new byte[4];
    bytesRead = stream.Read(length, 0, 4);
}

bytesRead contains the first 4 bytes stored in the networkstream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.