欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php使用pack处理二进制文件的方法

程序员文章站 2024-01-12 18:36:34
php读写二进制文件可以使用pack和unpack函数。 今天要处理一个二进制文件的问题,所以需要用一下,特意了解一下pack的用法,unpack用法与此类似。 简单来...

php读写二进制文件可以使用pack和unpack函数。
今天要处理一个二进制文件的问题,所以需要用一下,特意了解一下pack的用法,unpack用法与此类似。

简单来说,pack函数就是给一个目标格式,和相应的参数,就可以返回二进制数据了。

下面举例加以说明,对于四个整数:

pack("l4", 0,1,2,3)
pack("llll", 0,1,2,3)
pack("l", 0).pack("l", 1).pack("l", 2).pack("l", 3)

上面的处理结果是一样的,也就是说,format是描述后面的数据的格式。
至于具体format可以用什么,看看format characters就知道了。
比如一个30字符的 pack("a30", "//www.jb51.net") ,就是这个意思,很简单

pack函数的官方声明如下:

引用
pack
(php 3, php 4, php 5)

pack -- pack data into binary string
description
string pack ( string format [, mixed args [, mixed ...]] )

pack given arguments into binary string according to format. returns binary string containing data. 

the idea to this function was taken from perl and all formatting codes work the same as there, however, there are some formatting codes that are missing such as perl's "u" format code. the format string consists of format codes followed by an optional repeater argument. the repeater argument can be either an integer value or * for repeating to the end of the input data. for a, a, h, h the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string. currently implemented are

表格 1. pack() format characters

code description 
a nul-padded string 
a space-padded string 
h hex string, low nibble first 
h hex string, high nibble first 
c signed char 
c unsigned char 
s signed short (always 16 bit, machine byte order) 
s unsigned short (always 16 bit, machine byte order) 
n unsigned short (always 16 bit, big endian byte order) 
v unsigned short (always 16 bit, little endian byte order) 
i signed integer (machine dependent size and byte order) 
i unsigned integer (machine dependent size and byte order) 
l signed long (always 32 bit, machine byte order) 
l unsigned long (always 32 bit, machine byte order) 
n unsigned long (always 32 bit, big endian byte order) 
v unsigned long (always 32 bit, little endian byte order) 
f float (machine dependent size and representation) 
d double (machine dependent size and representation) 
x nul byte 
x back up one byte 
@ nul-fill to absolute position

看累了英文,下面来看看对应的中文解释:

引用
pack()函数的作用是:将数据压缩成一个二进制字符串。

a - nul-padded string 
a - nul- 字符串填满[padded string] 
a - space-padded string 
a - space- 字符串填满[padded string] 
h - hex string, low nibble first 
h – 十六进制字符串,低“四位元”[low nibble first] 
h - hex string, high nibble first 
h - 十六进制字符串,高“四位元”[high nibble first] 
c - signed char 
c – 带有符号的字符 
c - unsigned char 
c – 不带有符号的字符 
s - signed short (always 16 bit, machine byte order) 
s – 带有符号的短模式[short](通常是16位,按机器字节顺序) 
s - unsigned short (always 16 bit, machine byte order) 
s – 不带有符号的短模式[short](通常是16位,按机器字节排序) 
n - unsigned short (always 16 bit, big endian byte order) 
n -不带有符号的短模式[short](通常是16位,按大endian字节排序) 
v - unsigned short (always 16 bit, little endian byte order) 
v -不带有符号的短模式[short](通常是16位,按小endian字节排序) 
i - signed integer (machine dependent size and byte order) 
i – 带有符号的整数(由大小和字节顺序决定) 
i - unsigned integer (machine dependent size and byte order) 
i – 不带有符号的整数(由大小和字节顺序决定) 
l - signed long (always 32 bit, machine byte order) 
l– 带有符号的长模式[long](通常是32位,按机器字节顺序) 
l - unsigned long (always 32 bit, machine byte order) 
l – 不带有符号的长模式[long](通常是32位,按机器字节顺序) 
n - unsigned long (always 32 bit, big endian byte order) 
n – 不带有符号的长模式[long](通常是32位,按大edian字节顺序) 
v - unsigned long (always 32 bit, little endian byte order) 
v– 不带有符号的长模式[long](通常是32位,按小edian字节顺序) 
f - float (machine dependent size and representation) 
f –浮点(由大小和字节顺序决定) 
d - double (machine dependent size and representation) 
d – 双精度(由大小和字节顺序决定) 
x - nul byte 
x – 空字节[nul byte] 
x - back up one byte 
x- 后面一个字节[back up one byte] 
@ - nul-fill to absolute position 
@ - nul- 添加到一个绝对位置[absolute position]

示例代码如下:

<?php 
$code=array(
"username"=>array("a7","张三adfb12"),
"pass"=>array("a10","asdf*#1"),
"age"=>array("c","23"),
"birthday"=>array("i","19900101"),
"email"=>array("a50","www.jb51.net"));
$stream=join("\0",parkbyarr($code));
echo $stream,strlen($stream);
file_put_contents("1.txt",$stream);//将流保存起来便于下面读取
function parkbyarr($arr)
{
 $atarr=array();
 foreach ($arr as $k=>$v)
 {
 $atarr[]=pack($v[0],$v[1]);
 }
 return $atarr;
}
function getascill($str)
{
 $arr=str_split($str);
 foreach ($arr as $v)
 {
 echo $v,"=",ord($v),"\n";
 }
}
$code=array(
"username"=>array("a20"),
"pass"=>array("a10"),
"age"=>array("c"),
"birthday"=>array("i"),
"email"=>array("a50"));
$stream=file_get_contents("1.txt");
var_dump(parkbyarr($stream,$code));
function parkbyarr($str,$code)
{
 $arr=explode("\0",$str);
 $atarr=array();
 $i=0;
 foreach ($code as $k=>$v)
 {
 $atarr[$k]=unpack($v[0],$arr[$i]);
 $i++;
 }
 return $atarr;
}