【问题标题】:C#, BinaryReader, 'Pull apart' binary fileC#,BinaryReader,“拉开”二进制文件
【发布时间】:2013-06-27 08:07:08
【问题描述】:

我正在尝试使用 binaryreader 来“拆开”并索引一个二进制文件,但我需要一些帮助。 到目前为止我得到了这个:

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);
                    }
                }

但现在最困难的部分开始了,至少对我来说是这样。

该文件包含由 169 个字节组成的字节数组,由 ! (0x21) 字符。 对于每个字节数组,我需要将其拆分为由 16 位和 8 位组成的多个值。 我确切地知道索引并且它总是相同的。例如:索引 0+1 包含一个 U16 十进制值是速度,索引 16+17 包含一个 S16 十进制值是压力。以此类推,遍历每个 169 字节的数组。

我应该如何处理?

编辑:

我没有这个:

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;
                }

【问题讨论】:

    标签: c# binaryfiles binary-data


    【解决方案1】:

    只需按照它们在文件中的顺序从阅读器中读取值。示例:

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

    BinaryReader methods用于读取大多数数据类型。

    循环流示例:

    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
    }
    

    【讨论】:

    • 好的!我不知道它是如此简单。在我这样做之前,我需要隔离每个之间的字节数组!特点。如何在/使用 binaryreader 中做到这一点?
    • @Felix:你为什么认为你需要这样做?您可以这样做,但是您将拥有一个必须从中提取值的字节数组。只需读取 169 个字节,然后读取作为分隔符的字节,然后重复直到文件末尾。
    • 嗯,所以我必须创建一个循环,每次到达分隔符时重新启动读取?还是计算字节数?
    • @Felix:您应该跟踪从文件中读取的字节数,以便在每次迭代中读取 169 个字节,然后是分隔符。您不能在文件中查找每个 0x21 值,因为这也可能出现在 169 字节的数据中。例如,如果速度是 289,那将表示为两个字节 0x21 0x01。
    • 我明白你的意思。我不确定这样的循环应该是什么样子。此外,文件始终以分隔符开头。可以举个例子吗?
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2012-02-17
    • 2012-01-11
    • 1970-01-01
    • 2015-04-08
    • 2017-10-01
    相关资源
    最近更新 更多