如何阅读在C#中的二进制文件?二进制文件

2023-09-06 14:29:01 作者:果味怪叔叔

我有一个文本和二进制图像中存在的一个文件,我需要阅读从0到30的位置有关案文,并在31的位置将是二进制格式的图像。 什么是我必须遵循着手这个问题的步骤?

目前,我正尝试使用的FileStream 来读它,然后我移动的FileStream VAR一个 BinaryReader在如下图所示:

 的FileStream FS =新的FileStream(文件路径,FileMode.Open,FileAccess.Read)
BR BinaryReader在新= BinaryReader在(FS)
 

从那里走,我迷路了。

更新

好了,所以我现在可以看我的文件。 直到第30位是我的30弦,从第30位是位串这实际上是一个图像。 我不知道我怎么从第30位读取的字节数,然后保存图像! 有没有人有什么想法? 按照一个例子,从我的文件,你有一些IDEIA:

£ˆ‰¢@‰¢@¢–…@•…¦@„£@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.-///%<<??@[K}@k{M÷]kðñôôô}ù~øòLKóôòÿg 如何导出数据库中的二进制文件和web文件

请注意,即使是@@@是我的字符串,并从该图片将是一个字节。

解决方案

扩大对罗杰的回答了一下,有些code。

一个字符串始终是连接codeD在一些格式,阅读它,你需要(尤其是在使用二进制阅读器)知道编码。在许多情况下,这是显而易见的ASCII,您可以使用Encoding.ASCII.GetString来分析它,如果你得到意想不到的收获(奇怪的字符等),然后尝试另一种编码。

要分析你需要使用图像解析图像。 .NET有几个作为自己的GUI命名空间的一部分。在示例下面,我用从系统之一。绘图(Windows窗体),但类似的那些存在于WPF,有许多第三方库在那里。

 使用(VAR读卡器= BinaryReader在新(File.open方法(someFile,FileMode.Open))
{
    //假设你的字符串是纯ASCII编码:
    VAR MyString的= System.Text.Encoding.ASCII.GetString(reader.ReadBytes(30));
    //字节的其余部分是图像数据,使用图像库来处理它
    VAR MYIMAGE = System.Drawing.Image.FromStream(reader.BaseStream);
}
 

现在MSDN有关于一个谨慎使用BinaryReader在与会同BaseStream,但我相信,在上述情况下,你应该,因为你不使用的图像后的数据流是安全的。但留意的问题。 如果的失败,你可以随时读取的字节到一个新的字节[] 和创建这些字节一个新的MemoryStream。

编辑:

您在您的评论表示您的字符串是 EBCDIC 这不幸的是意味着你不能使用任何内置的编码脱code吧。快速谷歌搜索发现乔恩飞碟双向一个后在EBCDIC .NET编码类可能让你开始。这将从根本上给你 ebcdicEncoding.GetString(...);

I have a file that exists within a text and a binary image, I need to read from 0 to 30 position the text in question, and the position on 31 would be the image in binary format. What are the steps that I have to follow to proceed with that problem?

Currently, I am trying to read it using FileStream, and then I move the FileStream var to one BinaryReader as shown below:

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)
BinaryReader br = new BinaryReader(fs)

From there forward, I'm lost.

UPDATE

Alright, so I Can read my file now. Until the position 30 is my 30 string, from position 30 is the bit string Which is Actually an image. I wonder how do I read the bytes from position 30 and then save the images! Does anyone have any ideas? Follow an example from my file to you have some ideia:

£ˆ‰¢@‰¢@¢–"…@•…¦@„£@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.-///%<<??@[K}@k{M÷]kðñôôô}ù~øòLKóôòÿg

Note that even the @ @ @ is my string and from that the picture would be one byte.

解决方案

Expanding on Roger's answer a bit, with some code.

A string is always encoded in some format, and to read it you need to know that encoding (especially when using binary reader). In many cases, it's plain ASCII and you can use Encoding.ASCII.GetString to parse it if you get unexpected results (weird characters etc.) then try another encoding.

To parse the image you need to use an image parser. .NET has several as part of their GUI namespaces. In the sample below I'm using the one from System.Drawing (windows forms) but similar ones exists in WPF and there are many third party libraries out there.

using (var reader = new BinaryReader(File.Open(someFile, FileMode.Open))
{
    // assuming your string is in plain ASCII encoding:
    var myString = System.Text.Encoding.ASCII.GetString(reader.ReadBytes(30));
    // The rest of the bytes is image data, use an image library to process it
    var myImage = System.Drawing.Image.FromStream(reader.BaseStream);
}

Now MSDN has a caution about using the BaseStream in conjunction with BinaryReader but I believe in the above case you should be safe since you're not using the stream after the image. But keep an eye out for problems. If it fails, you can always read the bytes into a new byte[] and create a new MemoryStream from those bytes.

EDIT:

You indicated in your comment your string is EBCDIC which unfortunately means you cannot use any of the built in Encodings to decode it. A quick google search revealed a post by Jon Skeet on a EBCDIC .NET Encoding class that may get you started. It will essentially give you ebcdicEncoding.GetString(...);