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 / Interop / October 2004

Tip: Looking for answers? Try searching our database.

Convert parts of byte[] to struct

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amil Hanish - 13 Oct 2004 00:40 GMT
I have a huge byte array from another API that contains structs of differing
sizes.  I'd like to create n structs based on data in the byte array.  But
how?  For example, if I want to take bytes 40-56 and create struct Foo from
them...how do I do this?  You can assume I know how to create the properly
aligned struct.

Thanks

Amil
Cole - 13 Oct 2004 19:58 GMT
I'm not aware of any method that lets you go from a managed byte array to
struct.  Marshal.PtrToStructure takes an unmanaged pointer and changes it to
struct.  You could possibly try casting your byte[] as byte* (unsafe
required) and seeing if Marshal.PtrToStructure will handle it.

Other than that your stuck with something like this:

BinaryReader:
byte[] buffer = new byte[size];
s.Receive(buffer, size, 0);
MemoryStream bin = new MemoryStream(buffer);
BinaryReader din = new BinaryReader(bin);

/* Read each field using the reader's methods */

You could probably implement this generically using reflection.  If you do,
post us a copy :)

Cole

> I have a huge byte array from another API that contains structs of differing
> sizes.  I'd like to create n structs based on data in the byte array.  But
[quoted text clipped - 5 lines]
>
> Amil

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.