Question: I need to combine two Bytes into one int value.

I receive from my camera a 16bit Image were two successive bytes have the intensity value of one pixel. My goal is to combine these two bytes into one "int" vale. 

I manage to do this using the following code:

```
for (int i = 0; i < VectorLength * 2; i = i + 2)
{
  NewImageVector[ImagePointer] = ((int)(buffer.Array[i + 1]) << 8) | ((int)(buffer.Array[i]));      
  ImagePointer++;
}

```

My image is 1280\*960 so VectorLength==1228800 and the incomming buffer size is 2\*1228800=2457600 elements...

Is there any way that I can speed this up?
Maybe there is another way so I don't need to use a for-loop.

Thank you

Answer: 
Assuming you can (re-)define `NewImageVector` as a `short[]`, and every two consecutive bytes in `Buffer` should be transformed into a `short` (which basically what you're doing now, only you cast to an `int` afterwards), you can use [Buffer.BlockCopy](https://msdn.microsoft.com/en-us/library/system.buffer.blockcopy(v=vs.110).aspx) to do it for you.

As the documentation tells, you `Buffer.BlockCopy` copies bytes from one array to another, so in order to copy your bytes in buffer you need to do the following:

```
Buffer.BlockCopy(Buffer, 0, NewImageVector, 0, [NumberOfExpectedShorts] * 2)

```

This tells `BlockCopy` that you want to start copying bytes from `Buffer`, starting at index 0, to `NewImageVector` starting at index 0, and you want to copy `[NumberOfExpectedShorts] * 2` bytes (since every short is two bytes long). 

No loops, but it does depend on the ability of using a `short[]` array instead of an `int[]` array (and indeed, on using an array to begin with). 

Note that this also requires the bytes in `Buffer` to be in `little-endian` order (i.e. `Buffer[index]` contains the low byte, `buffer[index + 1]` the high byte).
You can achieve a small performance increase by using unsafe pointers to iterate the arrays. The following code assumes that `source` is the input byte array (`buffer.Array` in your case). It also assumes that `source` has an even number of elements. In production code you would obviously have to check these things.

```
int[] output = new int[source.Length / 2];
fixed (byte* pSource = source)
fixed (int* pDestination = output)
{
    byte* sourceIterator = pSource;
    int* destIterator = pDestination;
    for (int i = 0; i < output.Length; i++)
    {
        (*destIterator) = ((*sourceIterator) | (*(sourceIterator + 1) << 8));
        destIterator++;
        sourceIterator += 2;
    }
}
return output;

```