简体   繁体   English

如何在C#中使用BinaryReader一次读取二进制数据?

[英]How to read binary data at once using BinaryReader in C#?

all 所有

I am very new to handle with binary data. 我是处理二进制数据的新手。

I am looking into some codes from MSDN. 我正在研究MSDN中的一些代码。

Server side: 服务器端:

        string fileName = "AppSettings.dat";

        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }

And I received the binary file from server. 我从服务器收到了二进制文件。

        string clientFile = "AppSettings.dat";

        using (BinaryReader reader = new BinaryReader(File.Open(clientFile, FileMode.Open)))
        {
            float aspectRatio = reader.ReadSingle();
            string tempDirectory = reader.ReadString();
            int autoSaveTime = reader.ReadInt32();
            bool showStatusBar = reader.ReadBoolean();
        }

It seems pretty easy to read and write when you use BinaryWriter and BinaryReader class. 当您使用BinaryWriter和BinaryReader类时,似乎很容易读写。

But, here is my problem. 但是,这是我的问题。

The server developer left the company, and somehow nobody is able to contact to him. 服务器开发人员离开公司,以某种方式没有人能够与他联系。

I cannot figure out how he wrote to BinaryWriter. 我不知道他如何写信给BinaryWriter。

I mean the exact order. 我的意思是确切的顺序。

In the example, I have to read reader in order as 在示例中,我必须按顺序阅读Reader

ReadSingle 读单

ReadString 读字符串

ReadInt32 ReadInt32

ReadBoolean ReadBoolean

Then I can get the correct result. 然后我可以获得正确的结果。

But in my situation, I don't know the exact order. 但是在我的情况下,我不知道确切的顺序。

So, what I can try is how to read all the characters and numbers at once. 因此,我可以尝试的是如何一次读取所有字符和数字。

Like...... 喜欢......

string contents = reader.someMethod;

Then contents is like below: 然后内容如下:

1.250F
c:\Temp
10
true

The carriage return is skippable. 回车是可以跳过的。

EDIT 编辑

I have tried File.ReadAllBytes. 我试过File.ReadAllBytes。

But the result is not what I expected to be... 但是结果却不是我所期望的...

        byte[] bytes    = System.IO.File.ReadAllBytes(clientFile);
        string contents = Encoding.UTF8.GetString(bytes);

\\0\\0 ?\\ac:\\Temp\\n\\0\\0\\0 \\ 0 \\0 ?\\ ac:\\ Temp \\ n \\ 0 \\ 0 \\ 0

I worry that without knowing the correct order; 我担心不知道正确的顺序; then you're going to have a very hard time working this out. 那么您将很难解决这个问题。

What I would suggest is using a .net decompiler to read his code. 我建议使用.net反编译器读取他的代码。 I'm making the assumption that the code is owned by your company, so this probably isn't wrong to do so. 我假设代码是由您的公司拥有的,所以这样做可能不是错的。

RedGates reflector and Resharper both have facilities to decompile .net assemblies into C#. RedGates反射器和Resharper都具有将.net程序反编译为C#的功能。

There's also a free one called 'dotPeek' from Telerik which I've been informed is quite good but I've not used it myself. Telerik还提供了一个免费的名为“ dotPeek”的工具,据我所知它是相当不错的,但我自己并未使用它。

Essentially you just run the program and give it the assembly and you can browse the source code, as long as it's not been Obfuscated; 本质上,您只要运行该程序并为其提供程序集,就可以浏览源代码,只要未对其进行混淆即可; but even then - there are some operations that can't be obfuscated completely, such as the binaryWriter.Write as they have to happen in a specific order. 但是即使那样-有些操作也无法完全混淆,例如binaryWriter.Write,因为它们必须以特定的顺序发生。

If this isn't what you need, please try and clarify your question! 如果这不是您所需要的,请尝试澄清您的问题!

Hope this helps. 希望这可以帮助。

The code below writes a binary file, then reads it into a byte array and outputs the hexadecimal values. 下面的代码写入一个二进制文件,然后将其读入字节数组并输出十六进制值。

using (var writer = new BinaryWriter(File.Create("foo.bin")))
{
    writer.Write(1.250F);
    writer.Write(@"c:\Temp");
    writer.Write(10);
    writer.Write(true);
}

var bytes = File.ReadAllBytes("foo.bin");
var text = BitConverter.ToString(bytes);
Console.WriteLine(text);

Here's the output: 这是输出:

00-00-A0-3F-07-63-3A-5C-54-65-6D-70-0A-00-00-00-01

Decoding, we have: 解码,我们有:

0000A03f - 4 bytes = 1.250F

Next are 8 bytes that correspond to the string "c:\\Temp". 接下来是8个字节,它们对应于字符串“ c:\\ Temp”。 The first byte, 07 , is the length. 第一个字节07是长度。 BinaryWriter uses a 7-bit encoding scheme for the length, so strings shorter than 128 bytes only require a single length byte. BinaryWriter为长度使用7位编码方案,因此小于128个字节的字符串仅需要一个长度的字节。 The 7 characters that make up the string follow the length byte. 组成字符串的7个字符紧随长度字节。

Following that you have the four bytes 0A000000 , which is the integer 10. 接下来,您拥有四个字节0A000000 ,这是整数10。

Finally, you have the value 1, which is the boolean True. 最后,您具有值1,即布尔值True。

You should use BitConverter.ToString(bytes) to output the data from your file. 您应该使用BitConverter.ToString(bytes)从文件中输出数据。 Then you can examine the bytes directly. 然后,您可以直接检查字节。

If you have some idea of what's in the file, you can usually puzzle it out. 如果您对文件中的内容有所了解,通常可以将其弄乱。 Strings are easy to pick out, and you can often use them as anchors to figure out what's on either side of them. 字符串很容易挑选出来,您经常可以将它们用作定位符,以弄清楚它们两侧的内容。 This is especially true if you have knowledge of what's in the file and you're just trying to figure out the order in which things were written. 如果您了解文件中的内容,而您只是想弄清楚写入的顺序,则尤其如此。

You don't really need a decompiler to figure out the order he wrote things in. You can use ildasm.exe to examine the generated MSIL. 您实际上并不需要反编译器来弄清楚他写东西的顺序。您可以使用ildasm.exe来检查生成的MSIL。 The function calls to BinaryWriter.Write will be easy to spot, and the disassembled code will tell you whether it's writing a string, Boolean, integer, etc. For a one-time thing, that's the way I'd go. 该函数对BinaryWriter.Write将很容易发现,反汇编的代码将告诉您是否正在编写字符串,布尔值,整数等。对于一次性的事情,这就是我要这样做的方式。 If I had a larger job where I needed to understand a lot of code, I'd go with a better decompiler. 如果我的工作量较大,需要了解很多代码,那么我会选择一个更好的反编译器。

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

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