二进制读取器出现问题. [英] Problem with binary reader .

查看:116
本文介绍了二进制读取器出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析其规格如下的二进制文件

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



我为此使用了二进制阅读器以及ReadInt32和ReadString功能
这是代码示例 http://www.mediafire.com/?j86gt08p5sz6d9j [



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.

推荐答案

首先,您必须了解有关保存文件的程序的一些知识.它认为"int"是16位还是32位?这也可能会对浮动金额有多大影响.它会自动在字符串的末尾添加一个空字符吗?

找出这种情况的一种方法是获取其中包含已知值的文件,然后使用十六进制编辑器确切地找到您要查看的内容.之后,您可以编写适当的阅读器代码.
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.


感谢代码示例:JSOP是正确的,您不能假设Int是32位(您很幸运) ,而且您绝对不能假定.NET中的字符串与文件中的字符串"相同.

可能是文件中的字符串"实际上是一个以NULL终止的ASCII字符序列,而不是存储为二进制的.NET字符串.

我不会使用二进制读取器,而是将文件读取到字节数组中:
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();

然后,我将手动对其进行处理(在对它进行了很好的了解之后:您的描述通过将输入具体列为小尾数来暗示其中包含了大尾数和小尾数的组合)

在c中有一些代码示例?http://www.google.com/codesearch/p?hl=zh-CN#g5UVg0xXXBU/trunk/ghost/map.cpp&q=w3i&l=478是否有帮助? c#有类似getline的东西吗?"

是的,它确实具有行读取功能,但不是您所指的意思.您的getline由参数字符(在本例中为null)终止,这在标准C#AFAIK中不可用.基于.NET行的例程始终使用无法更改的System.Newline终止符.

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.


这篇关于二进制读取器出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆