Assuming you want the doubles placed in the corresponding byte array one after the other, LINQ can make short work out of this:
static byte[] GetBytes(double[] values)
{
return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
}
Alternatively, you could use Buffer.BlockCopy()
:
static byte[] GetBytesAlt(double[] values)
{
var result = new byte[values.Length * sizeof(double)];
Buffer.BlockCopy(values, 0, result, 0, result.Length);
return result;
}
To convert back:
static double[] GetDoubles(byte[] bytes)
{
return Enumerable.Range(0, bytes.Length / sizeof(double))
.Select(offset => BitConverter.ToDouble(bytes, offset * sizeof(double)))
.ToArray();
}
static double[] GetDoublesAlt(byte[] bytes)
{
var result = new double[bytes.Length / sizeof(double)];
Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
return result;
}