Anyone have something equivalent to (and prettier than) the following code?
private static float[] PairsOfBytesToFloats(byte[] bytes)
{
if (bytes.Length.IsNotAMultipleOf(2)) throw new ArgumentException();
float[] result = new float[bytes.Length / 2];
for (int i = 0; i < bytes.Length; i += 2)
{
result[i / 2] = BitConverter.ToUInt16(bytes, i);
}
return result;
}
Maybe a bit of LINQ:
return Enumerable.Range(0, bytes.Length/2)
.Select(index => (float)BitConverter.ToUInt16(bytes, index*2))
.ToArray();
我建议您使用Buffer.BlockCopy
方法而不是循环:
Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.