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 / April 2006

Tip: Looking for answers? Try searching our database.

HOWTO: Convert "Type" to a byte array and Back

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott - 06 Apr 2006 21:32 GMT
Hi All,

I am trying to convert an ArrayList (that can contain an array of any type)
to a byte array.
Using the BinaryWriter and BinaryReader is not helpful, for example since
the ArrayList can contain an array of doubles it is necessary to cast the
following:
   binWriter.Write((double)dblArrayList[0]);
However since the ArrayList can be of any type it is not possible (or is it)
to perform a cast at run time.

So what is desired is to have a mechanism that will perform conversions of
an ArrayList (of any type) to a byte array and to do the reverse, convert a
byte array back to an array list.
Note that the type is known by saving the type in a struct as a string I.E.:
"System.Double" along with the ArrayList.

Thanks for any help in advance.

Scott
Mattias Sjögren - 06 Apr 2006 21:37 GMT
Scott,

>So what is desired is to have a mechanism that will perform conversions of
>an ArrayList (of any type) to a byte array and to do the reverse, convert a
>byte array back to an array list.

Have you tried using the framework's serialization support?

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Scott - 07 Apr 2006 21:18 GMT
Mattias,

I was able to use the BinaryFormatter class along with the MemoryStream and
BinaryWriter classes to accomplish this task
The only casting necessary was when deserializing the ArrayList class back.

Thanks!
Scott

> Have you tried using the framework's serialization support?
>
> Mattias

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.