Problem to convert byte array to double

后端 未结 3 2108
暗喜
暗喜 2021-01-26 07:44

I have a problem to convert an byte array to double array using BitConverter.ToDouble().

Simply my program will select an image then convert the image to by

3条回答
  •  情话喂你
    2021-01-26 08:42

    BitConverter.ToDouble(byte[], int)
    

    uses eight bytes to construct a 64-bit double, which explains your problem (once you get to the 7th to last element, there are no longer eight bytes left). I'm guessing this is not what you want to do, based on how you set up your loop.

    I imagine you want something like:

    for(int i = 0; i < index; i++)
    {
        dImageArray[i] = (double)imageArray[i];
    }
    

    Edit - or using LINQ, just for fun:

    double[] dImageArray = imageArray.Select(i => (double)i).ToArray();
    

    On the other hand...

    If BitConverter is definitely what you want, then you'll need something like:

    double[] dImageArray = new double[imageArray.Length / 8];
    for (int i = 0; i < dImageArray.Length; i++)
    {
        dImageArray[i] = BitConverter.ToDouble(imageArray, i * 8);
    }
    

    Again, based on your code, I think the first solution is what you need.

提交回复
热议问题