Bhawna Tuteja

Bhawna Tuteja

  • NA
  • 544
  • 277.9k

Converting DoubleArray To IntPtr and then to Byte Array

Feb 15 2017 12:44 AM
Hi,
I have requirement to convert double array to IntPtr and this IntPtr to byte array.
I want to do the reversal also just confirm values stored in byte array is of correct int array.
The steps I am following is
 
double[] dmanagedArray = new double[length];
for (int intI = 0; intI < dmanagedArray.Length; intI++)
{
dmanagedArray[intI] = intI * 10;
}
int size = Marshal.SizeOf(dmanagedArray[0]) * dmanagedArray.Length;
IntPtr srcPtr1 = Marshal.AllocHGlobal(size);
// This will copy double array to IntPtr. 
Marshal.Copy(dmanagedArray, 0, srcPtr1, dmanagedArray.Length);
 
// Now let's convert this IntPtr to byte array
byte[] byteArray = new byte[dmanagedArray.Length] 
//Marshal.Copy(srcPtr1, byteArray, 0, byteArray.Length);
 
In my case what is happening is , byte[] now contains all the values as 0.
which I think is wrong. How to do right conversion. 
 

Answers (1)