php使用pack处理二进制文件的方法
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可以用什么,看看formatcharacters就知道了。
比如一个30字符的pack("a30","https://www.nhooo.com"),就是这个意思,很简单
pack函数的官方声明如下:
引用 pack (PHP3,PHP4,PHP5) pack--Packdataintobinarystring Description stringpack(stringformat[,mixedargs[,mixed...]]) Packgivenargumentsintobinarystringaccordingtoformat.Returnsbinarystringcontainingdata. TheideatothisfunctionwastakenfromPerlandallformattingcodesworkthesameasthere,however,therearesomeformattingcodesthataremissingsuchasPerl's"u"formatcode.Theformatstringconsistsofformatcodesfollowedbyanoptionalrepeaterargument.Therepeaterargumentcanbeeitheranintegervalueor*forrepeatingtotheendoftheinputdata.Fora,A,h,Htherepeatcountspecifieshowmanycharactersofonedataargumentaretaken,for@itistheabsolutepositionwheretoputthenextdata,foreverythingelsetherepeatcountspecifieshowmanydataargumentsareconsumedandpackedintotheresultingbinarystring.Currentlyimplementedare 表格1.pack()formatcharacters CodeDescription aNUL-paddedstring ASPACE-paddedstring hHexstring,lownibblefirst HHexstring,highnibblefirst csignedchar Cunsignedchar ssignedshort(always16bit,machinebyteorder) Sunsignedshort(always16bit,machinebyteorder) nunsignedshort(always16bit,bigendianbyteorder) vunsignedshort(always16bit,littleendianbyteorder) isignedinteger(machinedependentsizeandbyteorder) Iunsignedinteger(machinedependentsizeandbyteorder) lsignedlong(always32bit,machinebyteorder) Lunsignedlong(always32bit,machinebyteorder) Nunsignedlong(always32bit,bigendianbyteorder) Vunsignedlong(always32bit,littleendianbyteorder) ffloat(machinedependentsizeandrepresentation) ddouble(machinedependentsizeandrepresentation) xNULbyte XBackuponebyte @NUL-filltoabsoluteposition
看累了英文,下面来看看对应的中文解释:
引用 pack()函数的作用是:将数据压缩成一个二进制字符串。 a-NUL-paddedstring a-NUL-字符串填满[paddedstring] A-SPACE-paddedstring A-SPACE-字符串填满[paddedstring] h-Hexstring,lownibblefirst h–十六进制字符串,低“四位元”[lownibblefirst] H-Hexstring,highnibblefirst H-十六进制字符串,高“四位元”[highnibblefirst] c-signedchar c–带有符号的字符 C-unsignedchar C–不带有符号的字符 s-signedshort(always16bit,machinebyteorder) s–带有符号的短模式[short](通常是16位,按机器字节顺序) S-unsignedshort(always16bit,machinebyteorder) S–不带有符号的短模式[short](通常是16位,按机器字节排序) n-unsignedshort(always16bit,bigendianbyteorder) n-不带有符号的短模式[short](通常是16位,按大endian字节排序) v-unsignedshort(always16bit,littleendianbyteorder) v-不带有符号的短模式[short](通常是16位,按小endian字节排序) i-signedinteger(machinedependentsizeandbyteorder) i–带有符号的整数(由大小和字节顺序决定) I-unsignedinteger(machinedependentsizeandbyteorder) I–不带有符号的整数(由大小和字节顺序决定) l-signedlong(always32bit,machinebyteorder) l–带有符号的长模式[long](通常是32位,按机器字节顺序) L-unsignedlong(always32bit,machinebyteorder) L–不带有符号的长模式[long](通常是32位,按机器字节顺序) N-unsignedlong(always32bit,bigendianbyteorder) N–不带有符号的长模式[long](通常是32位,按大edian字节顺序) V-unsignedlong(always32bit,littleendianbyteorder) V–不带有符号的长模式[long](通常是32位,按小edian字节顺序) f-float(machinedependentsizeandrepresentation) f–浮点(由大小和字节顺序决定) d-double(machinedependentsizeandrepresentation) d–双精度(由大小和字节顺序决定) x-NULbyte x–空字节[NULbyte] X-Backuponebyte X-后面一个字节[Backuponebyte] @-NUL-filltoabsoluteposition @-NUL-添加到一个绝对位置[absoluteposition]
示例代码如下:
<?php $code=array( "username"=>array("A7","张三adfb12"), "pass"=>array("A10","asdf*#1"), "age"=>array("C","23"), "birthday"=>array("I","19900101"), "email"=>array("A50","www.nhooo.com")); $stream=join("\0",parkByArr($code)); echo$stream,strlen($stream); file_put_contents("1.txt",$stream);//将流保存起来便于下面读取 functionparkByArr($arr) { $atArr=array(); foreach($arras$k=>$v) { $atArr[]=pack($v[0],$v[1]); } return$atArr; } functiongetAscill($str) { $arr=str_split($str); foreach($arras$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)); functionparkByArr($str,$code) { $Arr=explode("\0",$str); $atArr=array(); $i=0; foreach($codeas$k=>$v) { $atArr[$k]=unpack($v[0],$Arr[$i]); $i++; } return$atArr; }