简体   繁体   English

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

[英]How do I read a binary file in C#?

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. 我有一个存在于文本和二进制图像中的文件,我需要从0到30的位置读取有问题的文本,并且31的位置将是二进制格式的图像。 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读取它,然后将FileStream var移动到一个BinaryReader ,如下所示:

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

From there forward, I'm lost. 从那以后,我迷路了。


UPDATE 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. 直到位置30是我的30字符串,从位置30开始是实际上是图像的位字符串。 I wonder how do I read the bytes from position 30 and then save the images! 我想知道如何从位置30读取字节,然后保存图像! 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. 在许多情况下,它是纯ASCII码,如果您得到意外的结果(奇怪的字符等),则可以使用Encoding.ASCII.GetString对其进行解析,然后尝试另一种编码。

To parse the image you need to use an image parser. 要解析图像,您需要使用图像解析器。 .NET has several as part of their GUI namespaces. .NET在其GUI名称空间中有多个。 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. 在下面的示例中,我使用的是System.Drawing (Windows窗体)中的一个,但WPF中存在类似的文件,并且那里有许多第三方库。

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. 现在,MSDN 对于将BinStream与BinaryReader结合使用已发出警告 ,但我相信在上述情况下,您应该是安全的,因为您不会在映像后使用流。 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. 如果失败,则始终可以将字节读取到新的byte[]并从这些字节创建新的MemoryStream。

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. 您在注释中指出您的字符串是EBCDIC ,很遗憾,这意味着您无法使用任何内置的Encodings对其进行解码。 A quick google search revealed a post by Jon Skeet on a EBCDIC .NET Encoding class that may get you started. 谷歌的快速搜索显示,乔恩·斯凯特(Jon Skeet)在EBCDIC .NET Encoding类上的帖子可能会让您入门。 It will essentially give you ebcdicEncoding.GetString(...); 从本质ebcdicEncoding.GetString(...); ,它将为您提供ebcdicEncoding.GetString(...);

You can use FileStream to open and read from the file. 您可以使用FileStream打开和读取文件。 If you read the first 30 bytes into a buffer you can then convert that to a string using "string Encoding.ASCII.GetString(byte[] buffer, int offset, int length)". 如果您将前30个字节读入缓冲区,则可以使用“ string Encoding.ASCII.GetString(byte []缓冲区,int偏移量,int长度)”将其转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM