Reading Binary Data.

A

Ady

Hi,

I am trying to create an editor and the structure of our existing files are
as follows.

In C# how would I read the following binary data from a file and convert it
into usable data.

// c code
typedef struct
{
float x0, y0, z0;
float x1, y1, z1;
float x2, y2, z2;
unsigned char r0,g0,b0;
} TRIANGLE;

typedef struct
{
char Id[4];
char version;
TRIANGLE[];
}

Any help appreciated

Ady.
 
J

Jay B. Harlow [MVP - Outlook]

Ady,
You can use Serialization or System.IO.BinaryReader.

With Serialization the system does most of the work for you, with
BinaryReader & BinaryWriter you have to do more of the work.

For details on Serialization see:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

For BinaryReader see:
http://msdn.microsoft.com/library/d.../html/frlrfSystemIOBinaryReaderClassTopic.asp

Hope this helps
Jay
 
A

Ady

Thanks for this.

I just have one more question if the format of the file is as follows,

typedef struct
{
float x0, y0, z0;
float x1, y1, z1;
float x2, y2, z2;
unsigned char r0,g0,b0;
} TRIANGLE;

typedef struct
{
char Id[4];
char Ver;
char header[90];
unsigned int offsetTextures;
TRIANGLE tri[];
} MyFile;

How could I pull out the value off offsetTextures and add it on to the
beginning of the file to get the offset to the start of my texture data.

an example would be much appreciated.

Ady.


Jay B. Harlow said:
Ady,
You can use Serialization or System.IO.BinaryReader.

With Serialization the system does most of the work for you, with
BinaryReader & BinaryWriter you have to do more of the work.

For details on Serialization see:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

For BinaryReader see:
http://msdn.microsoft.com/library/d.../html/frlrfSystemIOBinaryReaderClassTopic.asp

Hope this helps
Jay


Ady said:
Hi,

I am trying to create an editor and the structure of our existing files are
as follows.

In C# how would I read the following binary data from a file and convert it
into usable data.

// c code
typedef struct
{
float x0, y0, z0;
float x1, y1, z1;
float x2, y2, z2;
unsigned char r0,g0,b0;
} TRIANGLE;

typedef struct
{
char Id[4];
char version;
TRIANGLE[];
}

Any help appreciated

Ady.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top