PC Review


Reply
Thread Tools Rate Thread

Converting a double array to byte array

 
 
=?Utf-8?B?UmFncw==?=
Guest
Posts: n/a
 
      2nd Aug 2005
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      2nd Aug 2005
Rags <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?UmFncw==?=
Guest
Posts: n/a
 
      3rd Aug 2005
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



"Jon Skeet [C# MVP]" wrote:

> Rags <(E-Mail Removed)> wrote:
> > 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      3rd Aug 2005
Rags <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?UmFncw==?=
Guest
Posts: n/a
 
      3rd Aug 2005
That did it. Thanks so much for your help.

"Jon Skeet [C# MVP]" wrote:

> Rags <(E-Mail Removed)> wrote:
> > 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.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      10th Aug 2005
Great, I didn't know about this buffer class but it looks very interesting!

--
There are 10 kinds of people in this world. Those who understand binary and
those who don't.
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Rags <(E-Mail Removed)> wrote:
>> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a byte array to a ushort array fishspm@gmail.com Microsoft C# .NET 1 14th Oct 2006 11:24 AM
Converting byte array to short array? Ole Microsoft C# .NET 2 8th Jul 2006 01:28 AM
Howto convert a byte array to double array. kevinniu Microsoft C# .NET 5 19th Jul 2004 09:53 AM
converting an int array to a byte array Tomas Deman Microsoft Dot NET 2 9th Jul 2004 09:56 AM
How to convert a Byte array to a double array??? antonio matos Microsoft VB .NET 2 13th Nov 2003 03:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:35 AM.