gpt4 book ai didi

php - 在 Linux 上的二进制文件中提取到 x00 的特定偏移量之后的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:57 25 4
gpt4 key购买 nike

我正在寻找在Linux(命令行)上提取二进制文件中字符串的最简单方法。例如,在我的例子中,字符串以偏移量 138 开始,以第一个十六进制 00 结束。

最近几天我尝试使用 hexdump 并阅读了几次文档。遗憾的是,在我尝试的所有操作中,我只得到了十六进制值和字符串,而不是干净的字符串。

所以我的问题是,最简单的解决方案是什么?我应该更多地关注像 python、php 这样的脚本语言,还是有一些我不知道的东西可以更容易地实现它?

最佳答案

您可以简单地通过从偏移量 138 处的文件读取到缓冲区,直到到达 0x00 来完成此操作,如下所示...

// Open the file for read
$fp = fopen($fileName, "rb");
// Set the file pointer to a byte offset of 138 to begin reading
fseek($fp, 138);
$reached = false;
$buffer = "";
// Read into the buffer until we reac 0x00
do {
$buffer .= fread($fp, 8192);
$end = strpos($buffer, "\x00");
if ($end !== false || feof($fp)) {
$str = substr($buffer, 0, $end);
$reached = true;
}
} while(!$reached);

// $str will contain the string you're looking for

关于php - 在 Linux 上的二进制文件中提取到 x00 的特定偏移量之后的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37900616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com