初学者:将byte()转换/复制为single()的最快方法

时间:2020-03-06 15:04:25  来源:igfitidea点击:

由于Directx声音捕获,我返回了一个byte()数组,但是对于程序的其他部分,我想将结果视为single()。是逐项分解数组的最快方法吗?还是有巧妙的方法呢?

得到它的代码是

CType(Me._applicationBuffer.Read(Me._nextCaptureOffset, GetType(Byte), LockFlag.None, LockSize), Byte())

这将创建字节数组,Ctype是否可以处理单个? (请注意,我找不到解决方法!)

解决方案

尝试

float f = BitConverter.ToSingle(bytearray, 0);

在VB中(我认为):

Dim single s;
s = BitConverter.ToSingle(bytearray, 0);

public float[] ByteArrayToFloatArray(byte[] byteArray)
{
    float[] floatArray = new float[byteArray.Length / 4];
    for (int i = 0; i < floatArray.Length; i++)
    {
        floatArray[i] = BitConverter.ToSingle(byteArray, i * 4);
    }
    return floatArray;
}

最快的方法(就性能而言,而不是编写时间)可能是使用CopyMemory API调用。