• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

phppack、unpack、ord函数使用方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
string pack ( string $format [, mixed $args [, mixed $... ]] )

Pack given arguments into a binary string according to format.

The idea for this function was taken from Perl and all formatting codes work the same as in Perl. However, there are some formatting codes that are missing such as Perl's "u" format code.

Note that the distinction between signed and unsigned values only affects the function unpack(), where as function pack()gives the same result for signed and unsigned format codes.

参数 描述 
format 必需。规定在包装数据时所使用的格式。 
args+ 可选。规定被包装的一个或多个参数。 

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 formats are:

http://www.php.net/manual/zh/function.pack.php

字符 说明 
a 将字符串空白以 NULL 字符填满 
A 将字符串空白以 SPACE 字符 (空格) 填满 
h 十六进位字符串,低位在前 
H 十六进位字符串,高位在前 
c 有号字符 
C 无号字符 
s 有号短整数 (十六位,依计算机的位顺序) 
S 无号短整数 (十六位,依计算机的位顺序) 
n 无号短整数 (十六位, 高位在后的顺序) 
v 无号短整数 (十六位, 低位在后的顺序) 
i 有号整数 (依计算机的顺序及范围) 
I 无号整数 (依计算机的顺序及范围) 
l 有号长整数 (卅(释义:30)二位,依计算机的位顺序) 
L 无号长整数 (卅二位,依计算机的位顺序) 
N 无号短整数 (卅二位, 高位在后的顺序) 
V 无号短整数 (卅二位, 低位在后的顺序) 
f 单精确浮点数 (依计算机的范围) 
d 倍精确浮点数 (依计算机的范围) 
x 空位 
X 倒回一位 
@ 填入 NULL 字符到绝对位置 

php文档:http://php.net/manual/zh/function.pack.php

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)
q signed long long (always 64 bit, machine byte order)
Q unsigned long long (always 64 bit, machine byte order)
J unsigned long long (always 64 bit, big endian byte order)
P unsigned long long (always 64 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
Z NUL-padded string (new in PHP 5.5)
@ NUL-fill to absolute position

args

 

 

 

<?php
$binarydata 
pack("nvc*"0x12340x56786566);
?>

The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.

unpack函数说明:本函数用来将位的字符串的资料解压缩 

语法:unpack(format,args+) 

参数 描述 
format 必需。规定在包装数据时所使用的格式。 
args+ 可选。规定被包装的一个或多个参数。 

参数与pack相同。 

ord函数(ord函数:为序数函数,函数返回值为字符在ASCII码中的序号。)说明:返回对应字符的acill码值 

语法:ord($character); 
//A字符 
$str = (pack ( "A*", "中国" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//H字符 
$str = (pack ( "H*", "fffe" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//C字符 
$str = (pack ( "C*", "55", "56", "57" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//i字符 短整形 32位 4个字节 64位8个字节 
$str = (pack ( "i", "100" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//s字符 短整形 2个字节 
$str = (pack ( "s", "100" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//l字符 长整形 4个字节 
$str = (pack ( "l", "100" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//f字符 单精度浮点 4个字节 
$str = (pack ( "f", "100" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

//d字符 双精度浮点 8个字节 
$str = (pack ( "d", "100" ));
echo $str, "=", strlen ( $str ), "字节\n";
getAscill ( $str );
echo '<br/>';

function getAscill($str) {
    $arr = str_split ( $str );
    foreach ( $arr as $v ) {
        echo $v, "=", ord ( $v ), "\n";
    }
    echo "=============\r\n\r\n";
}

string

 '4xVAB' (length=6)

中国=6字节 ä=228 ¸=184 ­=173 å=229 ›=155 ½=189 ============= 
中国=6字节 ä=228 ¸=184 ­=173 å=229 ›=155 ½=189 ============= 
ÿþ=2字节 ÿ=255 þ=254 ============= 
789=3字节 7=55 8=56 9=57 ============= 
d=4字节 d=100 =0 =0 =0 ============= 
d=2字节 d=100 =0 ============= 
d=4字节 d=100 =0 =0 =0 ============= 
ÈB=4字节 =0 =0 È=200 B=66 ============= 
Y@=8字节 =0 =0 =0 =0 =0 =0 Y=89 @=64 ============= 

 

通过上面实例,我们可以看到,相同字符串,用不同格式存储,所占用的字节数不同。 这里也可以看到,以不同格式保存字符可以达存储节省空间。而且启到不可读加密效果。 突然想到一点,设计数据库字段类型问题,如果一个字段只是:10位长度整型。我们设置为整形:256*256*256*256 就4个字节,如果设置为10个长度字符串。那就占10个字节。整个消化空间就是2倍的。 设置正确字符类型对提高数据库性能有很多帮助。呵呵,有点跑题了…… 

php处理字节码通讯实例分析 
刚刚说的pack作用:节省空间、加密格式 

下面就这2个做一个实例说明,接口开发要求: 


参数 描述 
用户名 20字节,字符型 
密码 10字节,字符型 
年龄 1字节,无符char型 
出生年月 4字节,整型(19800101) 
邮箱 50字节,字符串 
各字段间用:"\0"分割 
A、PACK封包 

 
$code=array( 

"username"=>array("A20","张三adfb12"), 

"pass"=>array("A10","asdf*#1"), 

"age"=>array("C","23"), 

"birthday"=>array("I","19900101"), 

"email"=>array("A50","[email protected]")); 

$stream=join("\0",packByArr($code)); 

echo $stream,strlen($stream); 

 


file_put_contents("c:/1.txt",$stream);    //将流保存起来便于下面读取 

 

function packByArr($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"; 

         } 

} 

(在浏览器中用utf8编码查看:张三adfb12 asdf*#1 Ŧ/[email protected] 89; 文件内容:

张三adfb12         asdf*#1     Ŧ/ [email protected]     )

因为用”\0”分割,整个长度是89字节。通过上面输出,有一些字符串输出是可以读取的,其它都已经变成乱码了。这也是我说可以保密效果原因。 

B、Unpack解包 
解包需要按照,打包方式读取,该读取多长,该用什么类型读取,必须与打包规定一样。 

<?php
$code=array( 

"username"=>array("A20"), 

"pass"=>array("A10"), 

"age"=>array("C"), 

"birthday"=>array("I"), 

"email"=>array("A50")); 
$stream=file_get_contents("C:/1.txt");
var_dump(unpackByArr($stream,$code));

function unpackByArr($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;
}

参考:http://randomclan.blog.163.com/blog/static/1453009820125454418912/

 -------------------------------------------------------

案例一、pack实现缩减文件数据存储大小
[php]  view plain copy
 
  1. <?php  
  2. //存储整数1234567890  
  3. file_put_contents("test.txt", 1234567890);  
此时test.txt的文件大小是10byte。 注意此时文件大小是10字节,实际占用空间大小是1KB 。
 
 
上面存储的整数实际是以字符串形式存储于文件test.txt中。
但如果以整数的二进制字符串存jy储,将会缩减至4byte。
 
 
 
  1. <?php  
  2. print_r(unpack("i", file_get_contents("test.txt")));  
 
案例二、数据加密
以字符串形式存储一段有意义数据,7-110-abcdefg-117。
字符"-"分割后,第一位表示字符串长度,第二位表示存储位置,第三位表示实际存储的字符串,第四位表示结尾位置。
[php]  view plain copy
 
  1. <?php  
  2. file_put_contents("test.txt", "7-110-abcdefg-117");  
 
上述方法缺点:
一、数据存储大小
二、数据以明文方式存储,如果是任何敏感信息,都可能造成不安全访问。
三、文件存储大小,以不规则方式递增。
 
 
  
加密:
[php]  view plain copy
 
  1. <?php  
  2. file_put_contents("test.txt", pack("i2a7i1", 7, 110, "abcdefg", 117));  
 
存储一段数据,加密格式为:整数2位长度字符串10位长度整数1位长度。
优点:
一、数据大小最优化
二、在不知道"i2a7i1"这样的压缩格式时,即使拿到文件,也无法正确读出二进制文件转化为明文。
三、数据增加时,文件存储大小是等量递增。每次都是以19byte递增。
 
案例三、key-value型文件存储
存储生成的文件为两个:索引文件,数据文件
文件中数据存储的格式如下图:

 

 

<?php
error_reporting(E_ALL);

class fileCacheException extends Exception{

}

//Key-Value型文件存储
class fileCache{
     private $_file_header_size = 14;
     private $_file_index_name;
     private $_file_data_name;
     private $_file_index;//索引文件句柄
     private $_file_data;//数据文件句柄
     private $_node_struct;//索引结点结构体
     private $_inx_node_size = 36;//索引结点大小

     public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){
          $this->_node_struct = array(
               'next'=>array(1, 'V'),
               'prev'=>array(1, 'V'),
              'data_offset'=>array(1,'V'),//数据存储起始位置
              'data_size'=>array(1,'V'),//数据长度
              'ref_count'=>array(1,'V'),//引用此处,模仿PHP的引用计数销毁模式
              'key'=>array(16,'H*'),//存储KEY
          );

          $this->_file_index_name = $file_index;
          $this->_file_data_name = $file_data;

          if(!file_exists($this->_file_index_name)){
               $this->_create_index();
          }else{
               $this->_file_index = fopen($this->_file_index_name, "rb+");
          }

          if(!file_exists($this->_file_data_name)){
               $this->_create_data();
          }else{
               $this->_file_data = fopen($this->_file_data_name, "rb+");//二进制存储需要使用b
          }
     }

     //创建索引文件
     private function _create_index(){
          $this->_file_index = fopen($this->_file_index_name, "wb+");//二进制存储需要使用b
          if(!$this->_file_index) 
               throw new fileCacheException("Could't open index file:".$this->_file_index_name);

          $this->_index_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php标记防止下载
          $this->_index_puts($this->_file_header_size, pack("V1", 0));
     }


     //创建存储文件
     private function _create_data(){
          $this->_file_data = fopen($this->_file_data_name, "wb+");//二进制存储需要使用b
          if(!$this->_file_index) 
               throw new fileCacheException("Could't open index file:".$this->_file_data_name);

          $this->_data_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php标记防止下载
     }

     private function _index_puts($offset, $data, $length=false){
          fseek($this->_file_index, $offset);

          if($length)
          fputs($this->_file_index, $data, $length);
          else
          fputs($this->_file_index, $data);
     }

     private function _data_puts($offset, $data, $length=false){
          fseek($this->_file_data, $offset);
          if($length)
          fputs($this->_file_data, $data, $length);
          else
          fputs($this->_file_data, $data);
     }

     /**
     * 文件锁
     * @param $is_block 是否独占、阻塞锁
     */
     private function _lock($file_res, $is_block=true){
          flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB);
     }

     private function _unlock($file_res){
          flock($file_res, LOCK_UN);
     }

     public function add($key, $value){
          $key = md5($key);
          $value = serialize($value);
          $this->_lock($this->_file_index, true);
          $this->_lock($this->_file_data, true);

          fseek($this->_file_index, $this->_file_header_size);

          list(, $index_count) = unpack('V1', fread($this->_file_index, 4));

          $data_size = filesize($this->_file_data_name);

          fseek($this->_file_data, $data_size);

          $value_size = strlen($value);

          $this->_data_puts(filesize($this->_file_data_name), $value);

          $node_data = 
          pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $key);

          $index_count++;

          $this->_index_puts($this->_file_header_size, $index_count, 4);

          $this->_index_puts($this->get_new_node_pos($index_count), $node_data);

          $this->_unlock($this->_file_data);
          $this->_unlock($this->_file_index);
     }

     public function get_new_node_pos($index_count){
          return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1);
     }

     public function get_node($key){
          $key = md5($key);
          fseek($this->_file_index, $this->_file_header_size);
          $index_count = fread($this->_file_index, 4);

          if($index_count>0) {
               for ($i=0; $i < $index_count ; $i++) { 
                    fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i);
                    $data = fread($this->_file_index, $this->_inx_node_size);
                    $node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data);

                    if($key == $node['key']){
                         return $node;
                    }
               }
          }else{
               return null;
          }
     }

     public function get_data($offset, $length){
          fseek($this->_file_data, $offset);
          return unserialize(fread($this->_file_data, $length));
     }
}

//使用方法
$cache = new fileCache();
$cache->add('abcdefg' , 'testabc');
$data = $cache->get_node('abcdefg');
print_r($data);
echo $cache->get_data($data['data_offset'], $data['data_size']);
View Code

 

案例四、socket通信加密
通信双方都定义好加密格式:
例如:
$LOGIN = array(
     'COMMAND'=>array('a30', 'LOGIN'),
     'DATA'=>array('a30', 'HELLO')
);

$LOGOUT = array(
     'COMMAND'=>array('a30', 'LOGOUT'),
     'DATA'=>array('a30', 'GOOD BYE')
);

$LOGIN_SUCCESS = array(
     'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
     'DATA'=>array('V1', 1)
);

$LOGOUT_SUCCESS = array(
     'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
     'DATA'=>array('V1', time())
);

服务器端与客户端根据解析COMMAND格式,找到对应的DATA解码方式,得到正确的数据.

转自:http://blog.csdn.net/a600423444/article/details/8653827

详细:

http://my.oschina.net/goal/blog/195749

http://my.oschina.net/goal/blog/202378?p={{currentPage-1}}

一篇文章:

 socket字节流解析(网络抓包解析)

研究了一下PHP和C++socket通讯,用C++作为服务器端,php作为客户端进行.

socket通讯是基于协议的,因此,只要双方协议一致就行.

关于协议的选择:我看过网上大部分协议都是在应用层的协议,选用这样的协议很方便,基本上就是字符串传过来,传过去

本次研究的协议算是当今国际化的一个标准做法.length+flag+body(长度+类型+内容)的方式,

 

total_length code flag length1 string1 length2 string2
总长度 操作类型 标志 字符串1长度 字符串1 字符串2长度 字符串2
4字节 2字节 4字节(暂时无用) 2字节 x字节 2字节 x字节

 

 

php实现方式,也很容易,通过pack打包成二进制进行通讯.下面贴一下代码

本地测试主要应用为:发送账号和密码给服务器端

<?php
class Byte{
    //长度
    private $length=0;
    
    private $byte='';
    //操作码
    private $code;
    public function setBytePrev($content){
        $this->byte=$content.$this->byte;
    }
    public function getByte(){
        return $this->byte;
    }
    public function getLength(){
        return $this->length;
    }
    public function writeChar($string){
        $this->length+=strlen($string);
        $str=array_map('ord',str_split($string));
        foreach($str as $vo){
            $this->byte.=pack('c',$vo);
        }
        $this->byte.=pack('c','0');
        $this->length++;
    }
    public function writeInt($str){
        $this->length+=4;
        $this->byte.=pack('L',$str);
    }
    public function writeShortInt($interge){
        $this->length+=2;
        $this->byte.=pack('v',$interge);
    }
}
class GameSocket{
    private $socket;
    private $port=9991;
    private $host='192.168.211.231';
    private $byte;
    private $code;
    const CODE_LENGTH=2;
    const FLAG_LENGTH=4;
    public function __set($name,$value){
        $this->$name=$value;
    }
    public function __construct($host='192.168.211.231',$port=9991){
        $this->host=$host;
        $this->port=$port;
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if(!$this->socket){
            exit('创建socket失败');
        }
        $result = socket_connect($this->socket,$this->host,$this->port);
        if(!$result){
            exit('连接不上目标主机'.$this->host);
        }
        $this->byte=new Byte();
    }
    public function write($data){
        if(is_string($data)||is_int($data)||is_float($data)){
            $data[]=$data;
        }
        if(is_array($data)){
            foreach($data as $vo){
                $this->byte->writeShortInt(strlen($vo));
                $this->byte->writeChar($vo);
            }
        }
        
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
centos安装phpide(eclipse+php插件)发布时间:2022-07-10
下一篇:
nginxphprewrite配置发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap