
Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
> > 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.
Jon Skeet [C# MVP] - 03 Aug 2005 18:27 GMT
> 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
[quoted text clipped - 15 lines]
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Rags - 03 Aug 2005 19:37 GMT
That did it. Thanks so much for your help.
> > 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
[quoted text clipped - 21 lines]
> Doubles take up 8 bytes each, so just divide by 8 and it should be
> fine.
Great, I didn't know about this buffer class but it looks very interesting!

Signature
There are 10 kinds of people in this world. Those who understand binary and
those who don't.
>> Is there a way to convert an array of double to a byte array and vice
>> versa?
[quoted text clipped - 5 lines]
>
> You might want to have a look at Buffer.BlockCopy.