PHP
·
发表于 5年以前
·
阅读量:8296
php¶Áд¶þ½øÖÆÎļþ¿ÉÒÔʹÓÃpackºÍunpackº¯Êý¡£
½ñÌiÒª´¦ÀiÒ»¸o¶þ½øÖÆÎļþµÄÎÊÌa£¬ËuÒÔÐeÒªÓÃһϣ¬ÌØÒaÁ˽aÒ»ÏÂpackµÄÓ裬unpackÓèÓe´ËÀaËÆ¡£
¼oµ¥À´Ëµ£¬packº¯Êý¾ÍÊǸøÒ»¸oÄ¿±e¸ñʽ£¬ºÍÏaÓ¦µÄ²ÎÊý£¬¾Í¿ÉÒÔ*µ»Ø¶þ½øÖÆÊý¾ÝÁË¡£
ÏÂÃae¾ÙÀý¼ÓÒÔ˵Ã÷£¬¶ÔÓÚËĸoÕuÊý£º
pack("L4", 0,1,2,3)
pack("LLLL", 0,1,2,3)
pack("L", 0).pack("L", 1).pack("L", 2).pack("L", 3)
ÉÏÃaeµÄ´¦Ài½a¹uÊÇÒ»ÑuµÄ£¬Ò²¾ÍÊÇ˵£¬formatÊÇÃeÊoºoÃaeµÄÊý¾ÝµÄ¸ñʽ¡£
ÖÁÓÚ¾ßÌaformat¿ÉÒÔÓÃʲô£¬¿´¿´format characters¾ÍÖªµÀÁË¡£
±ÈÈçÒ»¸o30×Ö*uµÄ pack("a30", "http://www.jb51.net") £¬¾ÍÊÇÕa¸oÒa˼£¬ºÜ¼oµ¥
packº¯ÊýµÄ¹Ù*½ÉuÃ÷ÈçÏ£º
ÒýÓÃ
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
±i¸ñ 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
¿´ÀÛÁËÓ¢ÎÄ£¬ÏÂÃaeÀ´¿´¿´¶ÔÓ¦µÄÖÐÎĽaÊÍ£º
ÒýÓÃ
pack()º¯ÊýµÄ×÷ÓÃÊÇ£º½«Êý¾ÝѹËo³ÉÒ»¸o¶þ½øÖÆ×Ö*u´®¡£
a - NUL-padded string
a - NUL- ×Ö*u´®ÌiÂu[padded string]
A - SPACE-padded string
A - SPACE- ×Ö*u´®ÌiÂu[padded string]
h - Hex string, low nibble first
h ¨C Ê®Áu½øÖÆ×Ö*u´®£¬µÍ¡°ËÄλԪ¡±[low nibble first]
H - Hex string, high nibble first
H - Ê®Áu½øÖÆ×Ö*u´®£¬¸ß¡°ËÄλԪ¡±[high nibble first]
c - signed char
c ¨C ´øÓÐ*uºÅµÄ×Ö*u
C - unsigned char
C ¨C ²»´øÓÐ*uºÅµÄ×Ö*u
s - signed short (always 16 bit, machine byte order)
s ¨C ´øÓÐ*uºÅµÄ¶Ìģʽ[short]£¨Í¨³£ÊÇ16룬°´»uÆ÷×Ö½Ú˳Ðo£(C)
S - unsigned short (always 16 bit, machine byte order)
S ¨C ²»´øÓÐ*uºÅµÄ¶Ìģʽ[short]£¨Í¨³£ÊÇ16룬°´»uÆ÷×Ö½ÚÅÅÐo£(C)
n - unsigned short (always 16 bit, big endian byte order)
n -²»´øÓÐ*uºÅµÄ¶Ìģʽ[short]£¨Í¨³£ÊÇ16룬°´´oendian×Ö½ÚÅÅÐo£(C)
v - unsigned short (always 16 bit, little endian byte order)
v -²»´øÓÐ*uºÅµÄ¶Ìģʽ[short]£¨Í¨³£ÊÇ16룬°´Ð¡endian×Ö½ÚÅÅÐo£(C)
i - signed integer (machine dependent size and byte order)
i ¨C ´øÓÐ*uºÅµÄÕuÊý£¨ÓÉ´oСºÍ×Ö½Ú˳Ðo¾o¶¨£(C)
I - unsigned integer (machine dependent size and byte order)
I ¨C ²»´øÓÐ*uºÅµÄÕuÊý£¨ÓÉ´oСºÍ×Ö½Ú˳Ðo¾o¶¨£(C)
l - signed long (always 32 bit, machine byte order)
l¨C ´øÓÐ*uºÅµÄ³¤Ä£Ê½[long]£¨Í¨³£ÊÇ32룬°´»uÆ÷×Ö½Ú˳Ðo£(C)
L - unsigned long (always 32 bit, machine byte order)
L ¨C ²»´øÓÐ*uºÅµÄ³¤Ä£Ê½[long]£¨Í¨³£ÊÇ32룬°´»uÆ÷×Ö½Ú˳Ðo£(C)
N - unsigned long (always 32 bit, big endian byte order)
N ¨C ²»´øÓÐ*uºÅµÄ³¤Ä£Ê½[long]£¨Í¨³£ÊÇ32룬°´´oedian×Ö½Ú˳Ðo£(C)
V - unsigned long (always 32 bit, little endian byte order)
V¨C ²»´øÓÐ*uºÅµÄ³¤Ä£Ê½[long]£¨Í¨³£ÊÇ32룬°´Ð¡edian×Ö½Ú˳Ðo£(C)
f - float (machine dependent size and representation)
f ¨C¸¡µa£¨ÓÉ´oСºÍ×Ö½Ú˳Ðo¾o¶¨£(C)
d - double (machine dependent size and representation)
d ¨C Ë«¾«¶È£¨ÓÉ´oСºÍ×Ö½Ú˳Ðo¾o¶¨£(C)
x - NUL byte
x ¨C ¿Õ×Ö½Ú[NUL byte]
X - Back up one byte
X- ºoÃaeÒ»¸o×Ö½Ú[Back up one byte]
@ - NUL-fill to absolute position
@ - NUL- Ìi¼Óµ½Ò»¸o¾ø¶ÔλÖÃ[absolute position]
ʾÀý´uÂeÈçÏ£º
<?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);//½«Á÷±£´aeÆðÀ´±aÓÚÏÂÃae¶ÁÈ¡
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;
}