Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Converting a double array to byte array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rags - 02 Aug 2005 21:07 GMT
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
Jon Skeet [C# MVP] - 02 Aug 2005 22:55 GMT
> 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.

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 18:06 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
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.
Lloyd Dupont - 10 Aug 2005 13:22 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.