为什么不能像unpack("Lfffffff",$bytes)那样把很多条unpack语句压缩成一条?
Why can't compress many unpack statements into one as the form of unpack("Lfffffff",$bytes)?
<?php
//it is unnecessary to get the data file.
$handle = fopen('data', 'rb');
fread($handle,"64");
//it is no use to parse the first 64 bytes here.
$bytes= fread($handle,"4");
print_r(unpack("L",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
?>
我用代码得到了正确的输出。
Array ( [1] => 20150416 )
Array ( [1] => 1.0499999523163 )
Array ( [1] => 1.25 )
Array ( [1] => 1.0299999713898 )
Array ( [1] => 1.1900000572205 )
Array ( [1] => 509427008 )
Array ( [1] => 566125248 )
Array ( [1] => 509427008 )
现在想把很多unpack语句压缩成unpack("Lfffffff",$bytes)
形式,代码如下
<?php
$handle = fopen('data', 'rb');
fread($handle,"64");
//it is no use to parse the first 64 bytes here.
$bytes= fread($handle,"32");
print_r(unpack("Lfffffff",$bytes));
?>
为什么我的结果中只有一个输出,没有其他已解析的数据?如何解决?
Array ( [fffffff] => 20150416 )
数据文件是用notepad++打开的,通过插件--TextFX查看。
这里只解析了96个字节,fread省略了前64个字节。
来自unpack doc:
The unpacked data is stored in an associative array. To accomplish
this you have to name the different format codes and separate them by
a slash /. If a repeater argument is present, then each of the array
keys will have a sequence number behind the given name.
试试这个例子:
<?php
$array = array (20150416, 1.0499999523163, 1.25, 1.0299999713898, 1.1900000572205, 509427008, 566125248, 509427008);
$output = pack('L', $array[0]);
for($i = 1; $i < 8; $i++) {
$output .= pack('f', $array[$i]);
}
print_r(unpack("LL/f7", $output));
?>
在unpack("LL/f7", $output)
中第一个L
指向unsigned long第二个L
指向数组中的索引(见第一个元素在输出中)/
(阅读答案的第一部分)和 f
指的是 float,7
指的是七个浮点值。
输出:
Array
(
[L] => 20150416
[1] => 1.0499999523163
[2] => 1.25
[3] => 1.0299999713898
[4] => 1.1900000572205
[5] => 509427008
[6] => 566125248
[7] => 509427008
)
你的情况应该是:
<?php
$handle = fopen('data', 'rb');
$bytes= fread($handle,"32");
print_r(unpack("LL/f7",$bytes));
?>
<?php
//it is unnecessary to get the data file.
$handle = fopen('data', 'rb');
fread($handle,"64");
//it is no use to parse the first 64 bytes here.
$bytes= fread($handle,"4");
print_r(unpack("L",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
$bytes= fread($handle,"4");
print_r(unpack("f",$bytes));
echo "<br/>";
?>
我用代码得到了正确的输出。
Array ( [1] => 20150416 )
Array ( [1] => 1.0499999523163 )
Array ( [1] => 1.25 )
Array ( [1] => 1.0299999713898 )
Array ( [1] => 1.1900000572205 )
Array ( [1] => 509427008 )
Array ( [1] => 566125248 )
Array ( [1] => 509427008 )
现在想把很多unpack语句压缩成unpack("Lfffffff",$bytes)
形式,代码如下
<?php
$handle = fopen('data', 'rb');
fread($handle,"64");
//it is no use to parse the first 64 bytes here.
$bytes= fread($handle,"32");
print_r(unpack("Lfffffff",$bytes));
?>
为什么我的结果中只有一个输出,没有其他已解析的数据?如何解决?
Array ( [fffffff] => 20150416 )
数据文件是用notepad++打开的,通过插件--TextFX查看。 这里只解析了96个字节,fread省略了前64个字节。
来自unpack doc:
The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.
试试这个例子:
<?php
$array = array (20150416, 1.0499999523163, 1.25, 1.0299999713898, 1.1900000572205, 509427008, 566125248, 509427008);
$output = pack('L', $array[0]);
for($i = 1; $i < 8; $i++) {
$output .= pack('f', $array[$i]);
}
print_r(unpack("LL/f7", $output));
?>
在unpack("LL/f7", $output)
中第一个L
指向unsigned long第二个L
指向数组中的索引(见第一个元素在输出中)/
(阅读答案的第一部分)和 f
指的是 float,7
指的是七个浮点值。
输出:
Array
(
[L] => 20150416
[1] => 1.0499999523163
[2] => 1.25
[3] => 1.0299999713898
[4] => 1.1900000572205
[5] => 509427008
[6] => 566125248
[7] => 509427008
)
你的情况应该是:
<?php
$handle = fopen('data', 'rb');
$bytes= fread($handle,"32");
print_r(unpack("LL/f7",$bytes));
?>