Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to parse binary file whose specification looks like this
Format:
int: file format version = 18
int: number of saves (map version)
int: editor version (little endian)
String: map name TRIGSTR_<WHATEVER> 
String: map author TRIGSTR_<WHATEVER> 
String: map description TRIGSTR_<WHATEVER> 
String: players recommended
float[8]: "Camera Bounds" 
int[4]: camera bounds complements* (see note 1) (ints A, B, C and D)
int: map playable area width E* (see note 1)
int: map playable area height F* (see note 1)
   *note 1:
   map width = A + E + B
   map height = C + F + D


and I used binary reader for this and ReadInt32 and ReadString fuctions
here's the code sample http://www.mediafire.com/?j86gt08p5sz6d9j[^].
Stream stream = new MemoryStream(Properties.Resources.war3map);
     stream.Seek(0, SeekOrigin.Begin);
     BinaryReader reader = new BinaryReader(stream);
     listBox1.Items.Add(reader.ReadInt32()); //reeads propertly
     listBox1.Items.Add(reader.ReadInt32()); //OK
     listBox1.Items.Add(reader.ReadInt32()); //OK
     listBox1.Items.Add(reader.ReadString());// Reads RIGSTR_003 and should TRIGSTR_003
     listBox1.Items.Add(reader.ReadString()); //Doesn't read at all but should TRIGSTR_006
     listBox1.Items.Add(reader.ReadString()); //Doesn't also read but should TRIGSTR_005



For some reason it doesn't work please help.

Thanks in advance.
Posted
Updated 27-Feb-11 1:34am
v5
Comments
Dalek Dave 27-Feb-11 6:54am    
Edited for readability
OriginalGriff 27-Feb-11 7:11am    
There are quite a few possibilities here, but you don't help by saying "it doesn't work": it would help if we knew what it does or doesn't do.

Please edit your post and post the code fragment there, as I for one am not going to download random files from unknown people!

First, you have to know something about the program that saved the file. Does it think an "int" is 16 bits, or 32 bits? This may also have an impact on how big a float is. Does it au8tomatically add a null character onto the end of a string?

One way to find this out is to get a file that has known values in it,and then use a hex editor to find out EXACTLY what you're looking at. After that, you can write the appropriate reader code.
 
Share this answer
 
Comments
apaka 27-Feb-11 7:40am    
- Integers
Integers are 4 bytes in Little Endian Order
-Strings
Strings are just like arrays of chars finished with a null char (From C++ : "\0").
Thanks for the code sample: JSOP is right, you can't assume that an Int is 32 bits (you have been lucky), and you very definitely can't assume that a string in .NET is the same as a "string" in your file.

The chances are that a "string" in your file is actually a null terminated sequence of ASCII characters, rather than a .NET string stored as binary.

I would not use a binary reader, but read the file into a byte array:
stream stream = new MemoryStream(Properties.Resources.war3map);
byte[] data = stream.ToArray();
I would then process it manually (after I had had a good look at it: your description implies there is a mixture of big- and little-endian numbers in there by listing an input as specifically little-endian)

"Here some code sample in c? http://www.google.com/codesearch/p?hl=en#g5UVg0xXXBU/trunk/ghost/map.cpp&q=w3i&l=478 does it help does c# have something like getline?"

It does have line reading capabilities, yes - but not in the sense you mean. Your getline is terminated by a parameter character - in this case a null - which is not available in standard C# AFAIK. .NET line based routines always use a System.Newline terminator, which cannot be changed.
 
Share this answer
 
v2
Comments
CS2011 27-Feb-11 7:54am    
Good answer. My 5
apaka 27-Feb-11 7:55am    
Here some code sample in c? http://www.google.com/codesearch/p?hl=en#g5UVg0xXXBU/trunk/ghost/map.cpp&q=w3i&l=478 does it help does c# have something like getline?
OriginalGriff 27-Feb-11 8:13am    
Answer updated.
apaka 27-Feb-11 8:22am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900