巅云智能建站平台搭建版(创业流派版)火爆上线,毕生受权!新增:文章智能收罗+全站真静态打包+都会分站+智能小法式+不法词过滤+H5自顺应+智能链词等功效功效概况
赞助文档Help

php利用pack处置二进制文件的体例

一佰互联网站建造(www.taishanly.com) 宣布日期 2020-04-29 09:01:08 阅读数: 113

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.taishanly.com") ,便是这个意义,很简略

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.taishanly.com"));
$stream=join("",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),"
";
 }
}
$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("",$str);
 $atArr=array();
 $i=0;
 foreach ($code as $k=>$v)
 {
 $atArr[$k]=unpack($v[0],$Arr[$i]);
 $i++;
 }
 return $atArr;
}
一佰互联是天下着名建站品牌办事商,咱们有九年、网站建造、网页设想、php开辟和域名注册及假造主机办事经历,供给的办事更是天下着名。最近几年来还整合团队上风自立开辟了可视化多用户”“3.0平台版,拖拽排版网站建造设想,轻松完成pc站、手机微网站、小法式、APP一体化全网营销网站扶植 ,已胜利的为天下上百家收集公司供给自助建站平台搭建办事。

相干消息more

29
04月
PHP 字符串长度判定效力更高的体例

有经历的法式员发明,php判定字符串长度,利用isset()在速率上比strlen()更快,履行效力更高。即:复制代码 代码以下:$str ... >>概况

24
04月
CSS中filter属性的利用详解

filter 属性界说了元素的可视结果blur 给图象设置高斯恍惚。"radius"一值设定高斯函数的标准差,或... >>概况

20
04月
html5 web本地存储将代替咱们的coo

在html5中为咱们供给了一种本地缓存机制,它将代替咱们的cookie,并且它是不会随阅读器发会咱们的办事器真个。咱们能够接纳js在客户端自... >>概况

11
05月
专业网页设想的企业和单元

专业网页设想练习,具有本身的官方网站是现今很多公司中须要的一个关键,具有官网会完竣的兴修本身的营销收集,有助于增添官方网站产物品牌承认度... >>概况

高端网站扶植

美工统筹SEO,为企业电子商务营销助力!

德律风:

023-85725751
建站

产物

域名注册 假造主机 云办事器 企业邮局
智能建站 APP打包 微站/小法式 创业平台
网站推行 媒体营销 智能收罗 AI机械人
400德律风 短信营销 店销机械人
私家定制 流派网站