php使用pack处理二进制文件的方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
php使用pack处理二进制文件的方法
文章主要介绍了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","jb51"),就是这个意思,很简单pack函数的官方声明如下:
引用
pack
(PHP3,PHP4,PHP5)
pack--Packdataintobinarystring
Description
stringpack(stringformat[,mixedargs[,mixed...]])
Packgivenargumentsintobinarystringaccordingtoformat.Ret urnsbinarystringcontainingdata.
TheideatothisfunctionwastakenfromPerlandallformattingco desworkthesameasthere,however,therearesomeformattingcodes thataremissingsuchasPerl's"u"formatcode.Theformatstringconsis tsofformatcodesfollowedbyanoptionalrepeaterargument.Therep eaterargumentcanbeeitheranintegervalueor*forrepeatingtothee
ndoftheinputdata.Fora,A,h,Htherepeatcountspecifieshowmanyc haractersofonedataargumentaretaken,for@itistheabsolutepositi onwheretoputthenextdata,foreverythingelsetherepeatcountspec ifieshowmanydataargumentsareconsumedandpackedintotheres ultingbinarystring.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。