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

Tip: Looking for answers? Try searching our database.

Structure to byte array and back

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Allen - 09 Mar 2006 19:17 GMT
Is there a relatively painless method for converting the contents of a
structure into a byte array and back again?

I have been using Marshal to do this, but it is a pain! To get the byte
array I have to Marshal.StructureToPtr() and then use Marshal.ReadByte()
to extract each of the bytes into an array individually. The reverse is
just as painful.

Is there no general purpose (safe) mechanism for achieving the same thing?

-ken
Mattias Sjögren - 09 Mar 2006 19:37 GMT
Ken,

>and then use Marshal.ReadByte()
>to extract each of the bytes into an array individually.

You can use Marshal.Copy to get them all at once.

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.

Stuart Irving - 15 Mar 2006 17:42 GMT
try this:

MemoryStream ms = new MemoryStream();

BinaryFormatter bf = new BinaryFormatter();

bf.Serialize(ms, myStruct);

return ms.ToArray();

> Is there a relatively painless method for converting the contents of a
> structure into a byte array and back again?
[quoted text clipped - 7 lines]
>
> -ken
Peter Bromley - 15 Mar 2006 22:38 GMT
> Is there a relatively painless method for converting the contents of a
> structure into a byte array and back again?
[quoted text clipped - 7 lines]
>
> -ken

Well, as long as you are unconcerned with endian, versioning, or struct
layout issues, this C++ code below might help:

__value struct MyStruct
    {
    ...
    };

...
MyStruct myStruct;
...
// copy to Byte array
System::Byte dstBuffer __gc[] = new System::Byte[sizeof(MyStruct)];
System::Byte __pin* dstP = &dstBuffer[0];
*((MyStruct*)(void*)dstP) = myStruct;
...
// copy from byte array (srcBuffer)
System::Byte __pin* srcP = &srcBuffer[0];
MyStruct myStruct = *((MyStruct*)(void*)srcP);

I know it looks ugly but it does work assuming the constraints I
mentioned above.

Cheers, Peter

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.