如何在 C# 中读取二进制文件?
我有一个存在于文本和二进制图像中的文件,我需要从 0 到 30 位置读取相关文本,而 31 上的位置将是二进制格式的图像。 我必须遵循哪些步骤才能解决该问题?
目前,我正在尝试使用 FileStream
读取它,然后将 FileStream var 移动到一个 BinaryReader
,如下所示:
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)
BinaryReader br = new BinaryReader(fs)
从那里开始,我迷失了。
更新
好的,我现在可以读取我的文件了。 直到位置30是我的30字符串,从位置30开始是位串,它实际上是一个图像。 我想知道如何从位置 30 读取字节然后保存图像! 有人有什么想法吗? 按照我的文件中的示例,您会有一些想法:
£ˆ‰¢@‰¢@¢–”…@•…¦@„£@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.-///%<<??@[K}@k{M÷]kðñôôô}ù~øòLKóôòÿg
请注意,即使 @ @ @ 也是我的字符串,并且图片将是一个字节。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用一些代码对罗杰的答案进行了一些扩展。
字符串总是以某种格式进行编码,要读取它,您需要知道该编码(特别是在使用二进制读取器时)。在许多情况下,它是纯 ASCII,如果您得到意外结果(奇怪的字符等),您可以使用 Encoding.ASCII.GetString 来解析它,然后尝试其他编码。
要解析图像,您需要使用图像解析器。 .NET 有几个作为其 GUI 命名空间的一部分。在下面的示例中,我使用的是 System 中的一个。绘图(Windows 窗体),但 WPF 中也存在类似的绘图,并且有许多第三方库。
现在 MSDN 有一个关于使用 BaseStream 的警告与 BinaryReader 结合使用,但我相信在上述情况下您应该是安全的,因为您没有在图像之后使用流。但要留意问题。 如果失败,您始终可以将字节读入新的
byte[]
并从这些字节创建新的MemoryStream。编辑:
您在评论中指出您的字符串是EBCDIC,不幸的是这意味着您不能使用任何内置编码对其进行解码。通过 Google 快速搜索,发现了 Jon Skeet 发表的关于 EBCDIC .NET 编码类的文章,该类可能让你开始吧。它本质上会给你
ebcdicEncoding.GetString(...);
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.
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(...);
您可以使用 FileStream 打开并读取文件。如果将前 30 个字节读入缓冲区,则可以使用“string Encoding.ASCII.GetString(byte[] buffer, int offset, int length)”将其转换为字符串。
You can use FileStream to open and read from the file. 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)".