我寻求在linux (命令行)中提取二进制文件中字符串的最简单方法。例如,在我的示例中,字符串以偏移量138开头,以第一个十六进制00结尾。
在过去的几天里,我试着使用十六进制,并阅读了大约几次文档。可悲的是,在我尝试的所有东西中,我只得到了与字符串一起的十六进制值,而不是干净的字符串。
所以我的问题是,最简单的解决办法是什么?我应该更多地关注像python、php这样的脚本语言,还是有一些我不知道的东西可以更容易地实现呢?
发布于 2016-06-18 11:20:09
您可以简单地从偏移量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
https://stackoverflow.com/questions/37900616
复制相似问题