C# Arrays – How to Convert a Byte Array into an Int Array

arraysc++

How to Convert a byte array into an int array? I have a byte array holding 144 items and the ways I have tried are quite inefficient due to my inexperience. I am sorry if this has been answered before, but I couldn't find a good answer anywhere.

Best Answer

Simple:

//Where yourBytes is an initialized byte array.
int[] bytesAsInts = yourBytes.Select(x => (int)x).ToArray();

Make sure you include System.Linq with a using declaration:

using System.Linq;

And if LINQ isn't your thing, you can use this instead:

int[] bytesAsInts = Array.ConvertAll(yourBytes, c => (int)c);