phpʹÓÃpack´¦Àí¶þ½øÖÆÎļþµÄ·½·¨

5年以前  |  阅读数:311 次  |  编程语言:PHP 

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;
    }
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例