假設你想放置相應的字節數組一個在雙打前一後,LINQ可以做短期工作出這一點:
static byte[] GetBytes(double[] values)
{
return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
}
或者,你可以使用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;
}
要轉換回:
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;
}
你想要的值(即轉換。 '10.0' - > 10)還是底層機器表示的八個字節(例如序列化)? – Richard
我們在這裏談論什麼樣的轉換?你想獲取相應的double並將其「轉換」爲一個整型來獲取字節值?或者你想獲得每個double值的字節表示?你需要_clarify_。 –
的意圖是使用BinaryWriter.Write()函數。但是這隻接受字節。我想獲得每個double值的字節表示形式? – Raghaav