Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Code Newbie > Submit Tutorials > C#
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-23-2004, 09:30 AM   #1 (permalink)
pe666o
Registered User
 
pe666o's Avatar
 
Join Date: Apr 2004
Posts: 7
pe666o is on a distinguished road
Send a message via ICQ to pe666o
C# and advanced binary files

Well
in the previous tutorial everyone has seen how easy c# handles text files. You simply assoicate a stream with a particular file and read from it as if it is a string. Opening binary files is allmost the same, you just use BinaryReder / BinaryWriter to read and write to open File , i.e. stream.

However the operations you can perform on this stream is limited by the BinaryReader/Writer range of methods. You can read/write all the generic data types( e.g. int, float, byte, etc.) as well as byte arrays. But what about custom structs!? You can use the Serializable options in .NET bu that works with data types created in .NET. What if you have one binary files with some header, body, etc.

Well you might find the following functions very handy:


You supply an object, e.g your custom structure/class.
Returns: byte array, you can simply write using BinaryWriter in a file.

Code:
public static byte[] RawSerialize( object anything ) { int rawsize = Marshal.SizeOf( anything ); IntPtr buffer = Marshal.AllocHGlobal( rawsize ); Marshal.StructureToPtr( anything, buffer, false ); byte[] rawdatas = new byte[ rawsize ]; Marshal.Copy( buffer, rawdatas, 0, rawsize ); Marshal.FreeHGlobal( buffer ); return rawdatas; }
For the next one you supply the bytes read from BinaryReader as well as the type of your suctom structure to cast later to.
Returns object, representing you structure. Note: you must cast to your structure.

Code:
public static object RawDeserialize( byte[] rawdatas, Type anytype ) { int rawsize = Marshal.SizeOf( anytype ); if( rawsize > rawdatas.Length ) return null; IntPtr buffer = Marshal.AllocHGlobal( rawsize ); Marshal.Copy( rawdatas, 0, buffer, rawsize ); object retobj = Marshal.PtrToStructure( buffer, anytype ); Marshal.FreeHGlobal( buffer ); return retobj; }
Now you might need some example how to use the functions above in real life example.

Lets first have a custo structure:

Code:
[StructLayout(LayoutKind.Sequential)] struct SomeFileHeader { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)] public string Signature; public Int32 Offset; public Int32 Length; }
As you noticed you will need to use some attributes when defining your structures. By [StructLayout(LayoutKind.Sequential)] you tell .NET to arrange the data in sequence according to each struct.member position in it. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)] attribute is used as attribute for the string. You need to specifiy size and how to interprete the string. String is simply array of chars. Upon saving structs, the size of the struct must be known. But string variable is undefined unless you specifiy what will be the max size( SizeConst =4). About UnmanagedType.ByValTStr you should go and find more info in MSDN or simply experiment. In general i think this defines how .NET should interprete the string as unmanaged type.

At last here is an example(you should have already binaryReader assoc to a file):
Code:
//our custom structure SomeFileHeader someFileHeader =new SomeFileHeader(); //temp byte array buffer private byte[] buf =new byte[Marshal.SizeOf(SomeFileHeader )); //Read the header from the file stream and save it to an byte array buf = binaryReader.ReadBytes(Marshal.SizeOf(SomeFileHeader )); //Deserialization of the byte array (buf) to out PakHeader structure someFileHeader = (SomeFileHeader ) RawDeserialize(buf,someFileHeader .GetType());
After that you should be able to read all members from your struct.
I have this code working in one of my applications. So if you have any problems at all with the code above, please try to reach me.

All the best,
and good luck with .NEY

Creedits: some of the info above comes from article in published in MSDN.

Peter Petrov
pe666o
__________________
pe666o is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
java advanced imaging sheriff Java 1 09-03-2004 06:58 AM
Advanced CSS Admin HTML, XML, Javascript, AJAX 7 09-29-2003 10:26 PM
Flash Fans: Get Ready for 2 Advanced v.4 Prophecy sde Code Newbie News 7 07-17-2003 10:06 AM


All times are GMT -8. The time now is 02:19 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle