在 C# 中将字节数组转换为短数组

在 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.

我该怎么做?

一种可能性是使用 Enumerable.Select:

One possibility is using Enumerable.Select:

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

另一个是使用 Array.ConvertAll一个>:

Another is to use Array.ConvertAll:

byte[] bytes;
var shorts = Array.ConvertAll(bytes, b => (short)b);