C# 双数组到字节数组的转换

C# 双数组到字节数组的转换,c#,c#-3.0,C#,C# 3.0,如何将double[]数组转换为byte[]数组,反之亦然 class Program { static void Main(string[] args) { Console.WriteLine(sizeof(double)); Console.WriteLine(double.MaxValue); double[] array = new double[] { 10.0, 20.0, 30.0, 40.0 };

如何将
double[]
数组转换为
byte[]
数组,反之亦然

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(sizeof(double));
        Console.WriteLine(double.MaxValue);

        double[] array = new double[] { 10.0, 20.0, 30.0, 40.0 };
        byte[] convertedarray = ?

        Console.Read();
    }
}
使用该类


干杯。

我想你可以用这样的东西:

byte[] byteArray = new byteArray[...];  
...
byteArray.SetValue(Convert.ToByte(d), index);
你应该使用这个方法

看看页面示例,您将清楚地理解

doubleArray = byteArray.Select(n => {return Convert.ToDouble(n);}).ToArray();

您可以使用
Select
ToArray
方法将一个数组转换为另一个数组:

oneArray = anotherArray.Select(n => {
  // the conversion of one item from one type to another goes here
}).ToArray();
要将双精度转换为字节,请执行以下操作:

byteArray = doubleArray.Select(n => {
  return Convert.ToByte(n);
}).ToArray();
要将字节转换为双字节,只需更改转换部分:

doubleArray = byteArray.Select(n => {
  return Convert.ToDouble(n);
}).ToArray();

如果要将每个双精度表示转换为多字节表示,可以使用
SelectMany
方法和
BitConverter
类。由于每个双精度将产生一个字节数组,因此
SelectMany
方法将把它们展平为单个结果

byteArray = doubleArray.SelectMany(n => {
  return BitConverter.GetBytes(n);
}).ToArray();
要转换回双精度,您需要一次循环八个字节:

doubleArray = Enumerable.Range(0, byteArray.Length / 8).Select(i => {
  return BitConverter.ToDouble(byteArray, i * 8);
}).ToArray();

假设您希望将双精度数依次放入相应的字节数组中,LINQ可以对此进行简短的处理:

static byte[] GetBytes(double[] values)
{
    return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
}
或者,您可以使用
Buffer.BlockCopy()

要转换回:

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)还是基础机器表示的八个字节(例如用于序列化)?我们在这里讨论的是哪种转换?您想将相应的double和“cast”转换为整数类型以获得字节值吗?还是要获取每个双精度值的字节表示形式?您需要澄清。目的是使用BinaryWriter.Write()函数。但这只接受字节。我想得到每个双精度值的字节表示形式?@Raghaav:在这种情况下,您需要每个双精度值的字节表示形式。是的。如何做到这一点?数组大小将是100000如何将字节[]转换回双精度[]。这将从大双精度中删除一些数据。@VMAtm:但这不是谴责他的理由。这里的其他答案也有同样的问题,只是因为一开始还不清楚。BlockCopy版本很棒。谢谢你。
static byte[] GetBytes(double[] values)
{
    return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
}
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;
}