You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP搜索二进制文件中十六进制标签无预期输出问题

Hey there, let's dig into why your code isn't producing the expected output. I've spotted several issues that are causing the problem, and I'll walk you through fixing them step by step.

1. Incorrect File Path & Missing Error Checking

Your current path dirname(__FILE__). './file.bin' creates a messy path like [directory]/./file.bin — while PHP might resolve this, it's error-prone. Worse, you're not checking if file_get_contents actually succeeded (it returns false if the file doesn't exist or can't be read), which would silently break the rest of your code.

Fix this with a cleaner path and error handling:

$filePath = __DIR__ . '/file.bin';
$file = file_get_contents($filePath);
if ($file === false) {
    echo "Error: Could not read file at $filePath\n";
    return false;
}

2. Loop Boundary Causes Array Out-of-Bounds

Your loop runs for ($i = 0; $i < ($ori_hex_length - 1); $i++), but you're checking up to $i + 4 (5 total bytes). When i gets close to the end of the array, $i + 4 will be beyond the array's length, leading to undefined values and your nested if conditions never passing.

Adjust the loop to ensure there are enough elements left to check the full tag:

for ($i = 0; $i <= $ori_hex_length - 5; $i++) {

3. Redundant hexdec Calls & Messy Nested ifs

You're converting both array elements and target hex strings to decimal, but since $ori is already uppercase hex, you can compare strings directly. This is faster and easier to read. You can also replace nested ifs with a single condition using &&:

if ($ori_hex[$i] == "36" &&
    $ori_hex[$i+1] == "30" &&
    $ori_hex[$i+2] == "36" &&
    $ori_hex[$i+3] == "59" &&
    $ori_hex[$i+4] == "30") {

4. return false Blocks Your "Not Found" Message

When $string_loc == 0, you call return false; before echo "Not Found!"; — the echo will never run because return exits the function immediately. Swap the order to fix this.

5. Initial $string_loc Value Risks False Negatives

Setting $string_loc = 0 means if your tag is actually at position 0, your final check will incorrectly report "Not Found". Initialize it to -1 (an invalid position) instead.

6. Extra Curly Brace in the Match Block

There's an unnecessary opening curly brace { right before $string_loc = 0x00000000 + $i; — it doesn't break the code, but it's messy and can cause confusion. Remove it.

Full Corrected Code

<?php
// Author: Labrador
// Date: 19-07-2020
// Purpose: To modify the contents of a binary

checkBin();

// This function checks the input file to see if it is compatible with the program
function checkBin() {
    // Open file in current directory
    $filePath = __DIR__ . '/file.bin';
    $file = file_get_contents($filePath);
    
    // Check if file was read successfully
    if ($file === false) {
        echo "Error: Failed to read file at $filePath\n";
        return false;
    }

    echo "test 1\n";

    // Convert file to hexadecimal
    $ori = strtoupper(bin2hex($file));
    $ori_hex = str_split($ori, 2);
    $ori_hex_length = count($ori_hex);
    $string_loc = -1; // Initialize to invalid position

    echo "test 2\n";

    // Search for identification tag (36 30 36 59 30)
    for ($i = 0; $i <= $ori_hex_length - 5; $i++) {
        if ($ori_hex[$i] == "36" &&
            $ori_hex[$i+1] == "30" &&
            $ori_hex[$i+2] == "36" &&
            $ori_hex[$i+3] == "59" &&
            $ori_hex[$i+4] == "30") {
            
            $string_loc = $i;
            echo "Binary Identified as compatible Software <strong>0x" . dechex($string_loc) . "</strong>\n";
            // Uncomment below to stop searching after first match
            // break;
        }
    }

    // Return false if not found
    if ($string_loc == -1) {
        echo "Not Found!\n";
        return false;
    }

    echo "test 3\n";
}
?>

Quick Notes

  • I added dechex($string_loc) to output the position in proper hex format, which matches your original intent.
  • If you only care about the first occurrence of the tag, uncomment the break; line inside the match block.
  • Now you'll get clear feedback if the file can't be read, the tag is found, or it's missing.

内容的提问来源于stack exchange,提问作者Labrador

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:52:54