Converting a double array to byte array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to convert an array of double to a byte array and vice versa?
The bitconverter class allows convertion of a double value to a byte array,
but I didn't find a way to convert the entire array of double to byte array.
Is this even possible?? Any Suggestions?

Thanks
 
Rags said:
Is there a way to convert an array of double to a byte array and vice versa?
The bitconverter class allows convertion of a double value to a byte array,
but I didn't find a way to convert the entire array of double to byte array.
Is this even possible?? Any Suggestions?

You might want to have a look at Buffer.BlockCopy.
 
Thanks for pointing me to the buffer.bloackcopy. I wrote some sample code to
test how it works, I basically defined an array of double and coverted it to
byte array and finally converted the byte array back to an array of double.
What I noiced is that when I converted the byte array back to double, it
retuned an array of double which had more elements that the original array of
double. See the sample code below, any suggestions on how to get the original
double array back.

Dim dblarr() As Double = {2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0}

Dim test() As Byte = Array.CreateInstance(GetType(Byte),
Buffer.ByteLength(dblarr))
'Now copy the bytes from the double array to the byte array.
Buffer.BlockCopy(dblarr, 0, test, 0, Buffer.ByteLength(dblarr))

'Create a new double array to convert the byte array back to double
array
Dim dblNew() As Double = Array.CreateInstance(GetType(Double),
Buffer.ByteLength(test))

'Copy the bytes from byte array back to double array
Buffer.BlockCopy(test, 0, dblNew, 0, Buffer.ByteLength(test)) --->
This returned a double array of length 80, the first 10 elements returned
were correct, the rest of the 70 elements were 0s.

I hope I am clear.

Thanks
 
Rags said:
Thanks for pointing me to the buffer.bloackcopy. I wrote some sample code to
test how it works, I basically defined an array of double and coverted it to
byte array and finally converted the byte array back to an array of double.
What I noiced is that when I converted the byte array back to double, it
retuned an array of double which had more elements that the original array of
double. See the sample code below, any suggestions on how to get the original
double array back.

Dim dblarr() As Double = {2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0}

Dim test() As Byte = Array.CreateInstance(GetType(Byte),
Buffer.ByteLength(dblarr))
'Now copy the bytes from the double array to the byte array.
Buffer.BlockCopy(dblarr, 0, test, 0, Buffer.ByteLength(dblarr))

'Create a new double array to convert the byte array back to double
array
Dim dblNew() As Double = Array.CreateInstance(GetType(Double),
Buffer.ByteLength(test))

This line is the problem - you're creating a new double array with the
number of *bytes* in the byte array.

Doubles take up 8 bytes each, so just divide by 8 and it should be
fine.
 
That did it. Thanks so much for your help.

Jon Skeet said:
This line is the problem - you're creating a new double array with the
number of *bytes* in the byte array.

Doubles take up 8 bytes each, so just divide by 8 and it should be
fine.
 
Back
Top