使用位掩码检查二进制文件的信息

发布于 2024-10-21 20:20:51 字数 1563 浏览 5 评论 0原文

我被分配了以下编程任务(编辑为模糊的任务细节):

可以询问原始(二进制)文件(第二阶段实施所需)以检测 Pod 是否存在。格式取决于文件的来源 - FormatX 与 FormatY。使用 16 位字长,可以使用以下位掩码来确定文件中是否存在 pod:

Word #  Mask    Value   Indicates
1       0xFF00  0x8700  Little Endian (Format X)
1       0x00FF  0x0087  Big Endian (Format Y)
13      0x0200  0x0200  right pod installed (Format X)
13      0x0002  0x0002  right pod installed (Format Y)
13      0x0100  0x0100  left pod installed (Format X)
13      0x0001  0x0001  left pod installed (Format Y)

到目前为止我是如何解决这个问题的:我的本地文件系统上有该文件,因此我使用 System.IO.File .OpenRead 将其放入 Stream 对象中。我想一次读取 16 位/2 个字节的流。对于这种大小的第一个“字”,我尝试应用位掩码来检测我正在处理的格式。然后我向前跳​​到第 13 个“单词”,并根据该格式检测右/左 Pod。

这是我编写的一些初步代码,但不起作用。我知道我正在读取的文件应该是 Y 格式,但我的支票不起作用。

int chunksRead = 0;
int readByte;
while ((readByte = stream.ReadByte()) >= 0)
{
    chunksRead++;

    if (chunksRead == 1)
    {
        // Get format
        bool isFormatY = (readByte & 0x00FF) == 0x0087;
    }
    else if (chunksRead == 13)
    {
        // Check pods
    }
    else if (chunksRead > 13)
    {
        break;
    }
}

谁能看到我的实施有什么问题吗?我应该如何考虑 2 字节字长?

根据 @Daniel Hilgarth 的回复进行编辑
感谢您的快速回复,丹尼尔。我做了一个更改,现在它正在处理第一个单词:

byte[] rawBytes = new byte[stream.Length];
stream.Read(rawBytes, 0, rawBytes.Length);
ushort formatWord = Convert.ToUInt16(rawBytes[0] << 8 | rawBytes[1]);
bool formatCheck = (formatWord & 0x00FF) == 0x0087;

我只需要找到一个示例文件,该文件应该为安装的右/左 Pod 返回积极的结果来完成此任务。

I have been given the following programming task (edited to obscure mission-specifics):

The raw (binary) file (needed for Phase II implementation) can be interrogated to detect if pods are present. Format is dependent on the source of the file – FormatX vs. FormatY. Using a wordsize of 16 bits, the following bit masks can be used to determine presence of pods from the file:

Word #  Mask    Value   Indicates
1       0xFF00  0x8700  Little Endian (Format X)
1       0x00FF  0x0087  Big Endian (Format Y)
13      0x0200  0x0200  right pod installed (Format X)
13      0x0002  0x0002  right pod installed (Format Y)
13      0x0100  0x0100  left pod installed (Format X)
13      0x0001  0x0001  left pod installed (Format Y)

How I have approached this problem so far: I have the file on my local file system, so I use System.IO.File.OpenRead to get it into a Stream object. I want to read through the stream 16 bits/2 bytes at a time. For the first "word" of this size, I try applying bitmasks to detect what format I am dealing with. Then I skip forward to the 13th "word" and based on that format, detect right/left pods.

Here's some preliminary code I have written that is not working. I know the file I am reading should be Format Y but my check is not working.

int chunksRead = 0;
int readByte;
while ((readByte = stream.ReadByte()) >= 0)
{
    chunksRead++;

    if (chunksRead == 1)
    {
        // Get format
        bool isFormatY = (readByte & 0x00FF) == 0x0087;
    }
    else if (chunksRead == 13)
    {
        // Check pods
    }
    else if (chunksRead > 13)
    {
        break;
    }
}

Can anyone see what's wrong with my implementation? How should I account for the 2 byte wordsize?

Edit based on response from @Daniel Hilgarth
Thanks for the quick reply, Daniel. I made a change and it's working for the first word, now:

byte[] rawBytes = new byte[stream.Length];
stream.Read(rawBytes, 0, rawBytes.Length);
ushort formatWord = Convert.ToUInt16(rawBytes[0] << 8 | rawBytes[1]);
bool formatCheck = (formatWord & 0x00FF) == 0x0087;

I just need to find an example file that should return a positive result for right/left pod installed to complete this task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2024-10-28 20:20:51

您正在混合字节和单词。位置 13 处的表示安装的是左 Pod 还是右 Pod。您正在读取 12 个字节才能到达该位置,并且您正在检查第 13 个字节。这只是一半。
您的格式检查也存在同样的问题。您应该读取第一个单词(=2 字节)并检查它是否是所需的值之一。

要从读取的两个字节中获取一个字,可以使用位移运算符<<

ushort yourWord = firstByte << 8 | secondByte;

You are mixing bytes and words. The word at position 13 is about whether the left or the right pod is installed. You are reading 12 bytes to get to that position and you are checking the 13th byte. That is only half way.
The same problem is with your format check. You should read the first word (=2 bytes) and check whether it is one of the desired values.

To get a word from two bytes you read, you could use the bit shift operator <<:

ushort yourWord = firstByte << 8 | secondByte;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文