解析二进制文件 [英] Parsing Binary Files

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

问题描述

你好同伴们!


好​​吧,我想尝试用C#编写一个非常简单的应用程序。 (是我的第一个

计划)


我想做的是:


1)打开一个二进制文件

2)在此文件中搜索特定的字符串。

3)关闭文件


现在有什么特别的我应该做的事情,因为这是一个二进制文件?


任何代码示例都会非常感激。


谢谢


Hemang Shah

Hello fellow Coders!

ok, I"m trying to write a very simple application in C#. (Yes its my first
program)

What I want to do is :

1) Open a binary file
2) Search this file for a particular string.
3) Close the file

Now is there any special thing I should do as this is a binary file ?

Any code examples would very greating appreciated.

Thank You

Hemang Shah

推荐答案

Hemang Shah< v - ***** @ microsoft.com>写道:
Hemang Shah <v-*****@microsoft.com> wrote:
好吧,我试图在C#中编写一个非常简单的应用程序。 (是我的第一个
程序)

我想要做的是:

1)打开二进制文件
2)搜索此文件一个特定的字符串。
3)关闭文件

现在有什么特别的东西我应该做,因为这是一个二进制文件?
ok, I"m trying to write a very simple application in C#. (Yes its my first
program)

What I want to do is :

1) Open a binary file
2) Search this file for a particular string.
3) Close the file

Now is there any special thing I should do as this is a binary file ?




好​​吧,如果你想搜索一个*字符串*,你需要知道

编码 - 或者通过字符串你的意思是字节序列吗?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Well, if you''re trying to search for a *string*, you''ll need to know
the encoding - or by "string" do you mean "sequence of bytes"?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hello Jon


我正在尝试搜索OU =的出现次数在二进制文件中是的它是一个

字节序列。


如果我在hexviewer中打开文件,我可以看到它们并搜索它。相反

然后每次在hexviewer中打开文件,我想写一个实用程序

来搜索并显示它。


我确实在网上找到了一些以二进制模式打开文件并在文本框中显示

的代码。

但你在文本框中看到的是不一样的您在hexviewer中看到的内容。

此外,我不太了解代码。


这是代码:

void DisplayFile()

{


int nCols = 16;


FileStream inStream = new FileStream(selectedfile,FileMode.Open,

FileAccess.Read);


long nBytesToRead = inStream.Length;


if(nBytesToRead> 65536/4)


nBytesToRead = 65536/4;


int nLines =(int)(nBytesToRead / nCols)+ 1;


string [] lines = new string [nLines];


int nBytesRead = 0;


for(int i = 0; i< nLines; i ++)


{


StringBuilder nextLine = new StringBuilder();


nextLine.Capacity = 4 * nCols;


for(int j = 0; j< nCols; j ++)


{


int nextByte = inStream.ReadByte();


nBytesRead ++;


if(nextByte< 0 || nBytesRead> 65536)


休息;


char nextChar =(char)nextByte;


if(nextChar< 16)


nextLine.Append(" x0" + string .Format(" {0,1:X}",


(int)nextChar));


else if br />

(char.IsLetterOrDigit(nextChar)||


char.IsPunctuation(nextChar))


nextLine.Append("" + nextChar +"");


else


nextLine.Append(" x" + string.Format(" {0,2:X}",

(int)nextChar));


}


lines [i] = nextLine.ToString();


}


inStream.Close ();


this.textBoxContents.Lines = lines;


}


谢谢


__________________________________________________ ________________________


Hemang Shah MCSE A +

企业消息支持

直拨电话:(905)568-0434 x 23854


电子邮件: v - ***** @ microsoft.com


办公时间:美国东部时间19:00-06:00周三至周六。


" Jon Skeet [C#MVP] " < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Hello Jon

I''m trying to search for occurances of "OU=" in the binary file yes so its a
sequence of bytes.

If I open the file in hexviewer, I can see these and search for it. Rather
then opening up the file in hexviewer everytime, I want to write a utility
to search it and display it.

I did find some code online which opens the file in binary mode and displays
it on a text box.
But what you see in the text box is not the same what you see in hexviewer.
Moreover, I don''t really understand the code.

Here is the code:

void DisplayFile()

{

int nCols = 16;

FileStream inStream = new FileStream(chosenfile, FileMode.Open,

FileAccess.Read);

long nBytesToRead = inStream.Length;

if (nBytesToRead > 65536/4)

nBytesToRead = 65536/4;

int nLines = (int)(nBytesToRead/nCols) + 1;

string [] lines = new string[nLines];

int nBytesRead = 0;

for (int i=0 ; i<nLines ; i++)

{

StringBuilder nextLine = new StringBuilder();

nextLine.Capacity = 4*nCols;

for (int j = 0 ; j<nCols ; j++)

{

int nextByte = inStream.ReadByte();

nBytesRead++;

if (nextByte < 0 || nBytesRead > 65536)

break;

char nextChar = (char)nextByte;

if (nextChar < 16)

nextLine.Append(" x0" + string.Format("{0,1:X}",

(int)nextChar));

else if

(char.IsLetterOrDigit(nextChar) ||

char.IsPunctuation(nextChar))

nextLine.Append(" " + nextChar + " ");

else

nextLine.Append(" x" + string.Format("{0,2:X}",

(int)nextChar));

}

lines[i] = nextLine.ToString();

}

inStream.Close();

this.textBoxContents.Lines = lines;

}

Thank You

__________________________________________________ ________________________

Hemang Shah MCSE A+
Enterprise Messaging Support
Direct phone: (905) 568-0434 x 23854

Email: v-*****@microsoft.com

Office hours: Wed to Sat from 19:00-06:00 hrs EST.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Hemang Shah< v - ***** @ microsoft.com>写道:
Hemang Shah <v-*****@microsoft.com> wrote:
好吧,我试图在C#中编写一个非常简单的应用程序。 (是的,我的第一个
程序)

我想做的是:

1)打开一个二进制文件
2)在这个文件中搜索特定的字符串。
3)关闭文件

现在有什么特别的东西我应该做,因为这是一个二进制文件?
ok, I"m trying to write a very simple application in C#. (Yes its my
first
program)

What I want to do is :

1) Open a binary file
2) Search this file for a particular string.
3) Close the file

Now is there any special thing I should do as this is a binary file ?


<嗯,如果你想搜索一个*字符串*,你需要知道编码 - 或者通过字符串。你的意思是字节序列吗?

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要也寄给我



Well, if you''re trying to search for a *string*, you''ll need to know
the encoding - or by "string" do you mean "sequence of bytes"?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Hemang Shah< v - ***** @ microsoft.com>写道:
Hemang Shah <v-*****@microsoft.com> wrote:
我正在尝试搜索OU =的出现次数。在二进制文件中是的所以它是一个字节序列。


但是OU =是一系列*字符*。你是说你在寻找

形成OU =的ASCII编码的字节序列?我b $ b怀疑'你在追求什么。

如果我在hexviewer中打开文件,我可以看到这些并搜索它。相反,然后每次在hexviewer中打开文件,我想编写一个实用程序来搜索并显示它。

我确实在网上找到了一些打开文件的代码二进制模式并在文本框中显示它。
但你在文本框中看到的内容与你在hexviewer中看到的不一样。
而且,我真的不明白代码。
I''m trying to search for occurances of "OU=" in the binary file yes so its a
sequence of bytes.
But OU= is a sequence of *characters*. Do you mean you''re looking for
the sequence of bytes which form the ASCII encoding for "OU="? I
suspect that''s what you''re after.
If I open the file in hexviewer, I can see these and search for it. Rather
then opening up the file in hexviewer everytime, I want to write a utility
to search it and display it.

I did find some code online which opens the file in binary mode and displays
it on a text box.
But what you see in the text box is not the same what you see in hexviewer.
Moreover, I don''t really understand the code.




第一件事是放弃代码。它在许多方面都很糟糕。


我现在没有时间为你写一些示例代码,但我会

明天下午试试。基本上,你应该以块的形式阅读文件,

,然后查看正确的序列,知道它可能会超过块边界。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要也寄给我



The first thing is to ditch that code. It''s bad in many, many ways.

I don''t have time to write some sample code for you right now, but I''ll
try tomorrow afternoon. Basically, you should read the file in chunks,
and then look through for the correct sequence, knowing that it might
go across a "chunk boundary".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于解析二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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