使用php从二进制文件中提取数据 [英] Extracting data from binary files with php

查看:358
本文介绍了使用php从二进制文件中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从二进制文件中提取一些数据.数据以十六进制标记开头. 我必须先找到该标记,然后再提取x字节.

I have to extract some data from a binary file. the data begin with a hex marker. I have first to find this marker and then extract the x bytes after.

PHP非常适合文本文件操作.

PHP is good for text file manipulation.

有人对二进制数据有一个好主意吗?

Someone have a good idea to do that with binary data ?

谢谢!

推荐答案

您不需要任何特殊功能,例如pack或unpack等...尽管pack对于指定要搜索的针头可能很有用. php不会对字符串应用任何字符编码,而是直接保留它们,因此默认情况下它是二进制友好的.

You don't need any special functions like pack or unpack etc...although pack may be useful for specifying the needle to search for. php doesn't apply any character encoding to strings, it leaves them as is, so it's binary friendly by default.

$hexMarker = 0x70000000;// or whatever
$binaryData = file_get_contents($filename);
$x = 5;
$pos = strpos($binaryData, $hexMarker);
if ($pos !== false) {
    $start = $pos + strlen($hexMarker);
    echo substr($binaryData, $start, $x);
}

您可以考虑使用 fopen

you may consider using fopen and fread in an iterative fashion if the size of the file is large, as file_get_contents would consume a lot of memory in that case. But that's a separate question anyway.

这篇关于使用php从二进制文件中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆