我需要序列化一堆float
s 并在必要时转换为小端。我知道BitConverter.GetBytes(float)
,但我宁愿避免在 GC 堆上分配大量的 4 字节小数组。如何转换为byte[]
具有偏移索引的现有大数组?我想要类似的东西:
float[] theFloats; // filled up somewhere
byte[] theBytes = new byte[theFloats.Length * 4];
int offset = 0;
for (int i = 0; i < numFloats; ++i)
{
MagicClass.CopyFloatToBytes(theFloats[i], theBytes, offset);
offset += 4;
}