在C#中从double []转换为long

时间:2015-06-18 12:52:21

标签: c#

我宣布:

double[] DataBuff1 = Enumerable.Repeat((double)0, 50000).ToArray();
double[] DataBuff2 = Enumerable.Repeat((double)0, 50000).ToArray();

然后使用具有以下输入的函数:

rc = UploadData(ref (long)(DataBuff1), ref (long)(DataBuff2), ref NodeCnt, ref  NodeDataPointsPerPacket, ref TotalDataSize);

功能是:

private static int UploadData(ref long[] p1, ref long[] p2, ref int NodeCnt, ref int NodeDataPointsPerPacket, ref int TotalDataSize)

我在ref (long)(DataBuff1)上有错误:

  

无法将double[]类型转换为long

你能帮我吗?

2 个答案:

答案 0 :(得分:0)

我假设您正在尝试将整个DataBuff1DataBuff2双数组转换为长数组。

public static long[] ToLongArr(double[] arr)
{
     return arr.Select(d => (long)d).ToArray();
}

并使用如下:

rc = UploadData(ref ToLongArr(DataBuff1), ref ToLongArr(DataBuff2), ref NodeCnt, ref  NodeDataPointsPerPacket, ref TotalDataSize);

答案 1 :(得分:0)

只需将您的代码更改为:

long[] dataBuff1 = new long[50000];
long[] dataBuff2 = new long[50000];
rc = UploadData(ref dataBuff1, ref dataBuff2, ref NodeCnt, ref  NodeDataPointsPerPacket, ref TotalDataSize);

double是浮点数数据类型,而long是整数数据类型;两者都有64位大小。将long[]作为缓冲区比double[]更容易处理。使用new创建的数组的每个元素都使用默认值进行隐式初始化 - 数字类型为0,对象类型为null(请参阅default(T)