williamtroup williamtroup - 1 year ago 207
C# Question

Convert byte array to short array in C#

I'm currently reading a file and wanted to be able to convert the array of bytes obtained from the file into a short array.

How would I go about doing this?

Answer Source

One possibility is using Enumerable.Select:

byte[] bytes;
var shorts = bytes.Select(b => (short)b).ToArray();

Another is to use Array.ConvertAll:

byte[] bytes;
var shorts = Array.ConvertAll(bytes, b => (short)b);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download