简体   繁体   English

C#,BinaryReader,“拉开”二进制文件

[英]C#, BinaryReader, 'Pull apart' binary file

I'm trying to use binaryreader to 'pull apart' and index a binary file but I need some help. 我正在尝试使用binaryreader来“拉开”并索引二进制文件,但我需要一些帮助。 I got this so far: 到目前为止,我得到了:

using (BinaryReader b = new BinaryReader(File.Open(openRaw.FileName, FileMode.Open)))
                {
                    int pos = 0;
                    // 2A.
                    // Use BaseStream.
                    int length = (int)b.BaseStream.Length;
                    while (pos < length)
                    {


                        //Move on
                        pos += sizeof(int);
                    }
                }

But now the hard part starts, atleast for me. 但是现在困难的部分开始了,对我来说至少。

The file contains byte arrays consisting of 169 bytes seperated by a ! 该文件包含由!分隔的169个字节组成的字节数组。 (0x21) character. (0x21)字符。 For each byte array I need to split it into multiple values consisting of 16 and 8 bits. 对于每个字节数组,我需要将其拆分为由16位和8位组成的多个值。 I know the index exactly and it is always the same. 我完全知道该索引,并且始终相同。 Eg: Index 0+1 contains a U16 decimal value being speed, index 16+17 contains a S16 decimal value being pressure. 例如:索引0 + 1包含一个U16十进制值,即速度,索引16 + 17包含一个S16十进制值,即压力。 And so on through each array of 169 bytes. 依次遍历每个169个字节的数组。

How should I handle this? 我该如何处理?

EDIT: 编辑:

I no have this: 我没有这个:

FileInfo f = new FileInfo(openRaw.FileName);
                double s1 = Convert.ToDouble(f.Length);
                double s2 = s1 / 170;
                double s3 = Math.Floor(s2);
                double s4 = s3 * 170;

                using (BinaryReader b = new BinaryReader(File.Open(openRaw.FileName, FileMode.Open), Encoding.ASCII))
                {
                    int pos = 0;
                    // 2A.
                    // Use BaseStream.

                    while (pos < s4)
                    {

                         while (b.PeekChar() != -1)
                        {

                            if (b.ReadByte() != 0x21)
                            {
                                flag = 1;
                            }                                

                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            seconds = BitConverter.ToUInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            PW1 = BitConverter.ToUInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            PW2 = BitConverter.ToUInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            RPM = BitConverter.ToUInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            advance = BitConverter.ToInt16(temp, 0);
                            NN = b.ReadBytes(6);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            Baro = BitConverter.ToInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            map = BitConverter.ToInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            mat = BitConverter.ToInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            clt = BitConverter.ToInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            tps = BitConverter.ToInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            bat = BitConverter.ToInt16(temp, 0);
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            AFR = BitConverter.ToInt16(temp, 0);
                            stemp16 = b.ReadInt16();
                            temp = b.ReadBytes(2);
                            Array.Reverse(temp);
                            knock = BitConverter.ToInt16(temp, 0); ;  //33
                            NN = b.ReadBytes(135);

                            table1.Rows.Add((seconds * 0.00390625), RPM, map, (tps * 0.1), ((mat - 320) * 0.05555), ((clt - 320) * 0.05555), (PW1 * 0.000666), (PW2 * 0.000666), (advance * 0.1), (knock * 0.1), (RPM/100), (Baro * 0.1), (AFR * 0.1), (bat * 0.1));

                        }

                        //Move on
                        pos = pos+= sizeof(int);
                    }
                    dataGridView1.DataSource = table1;
                }

Just read the values from the reader in the order they come in the file. 只需按照它们在文件中出现的顺序从阅读器中读取值即可。 Example: 例:

ushort speed = b.ReadUInt16();
byte something = b.ReadByte();
sbyte someOther = b.ReadSByte();
short pressure = b.ReadInt16();

There are BinaryReader methods for reading most data types. BinaryReader方法可读取大多数数据类型。

Example of looping the stream: 循环播放流的示例:

while (b.PeekChar() != -1) {
  if (b.ReadByte() != 0x21) {
    // error - didn't find valid separator
  }
  ushort speed = b.ReadUInt16();
  // and read another 167 bytes of data
}

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

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