PHP 二进制 pack() unpack()
2016-01-18 20:28
776 查看
PHP 5.6.3 此版本加入"q", "Q", "J",低于此版本均不能使用 #5.6.3 The "q", "Q", "J" and "P" codes were added to enable working with 64-bit numbers. #5.5.0 The "Z" code was added with equivalent functionality to "a" for Perl compatibility.
参数列表 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
关于Pack: string pack ( string $format [, mixed $args [, mixed $... ]] ) 下面的第一部分把数字值包装成字节: $out = pack("CCCC", 65, 66, 67, 68); # $out 等于"ABCD" $out = pack("C4", 65, 66, 67, 68); # 一样的东西 下面的对 Unicode 的循环字母做同样的事情: $foo = pack("U4", 0x24b6, 0x24b7, 0x24b8, 0x24b9); 下面的做类似的事情,增加了一些空: $out = pack("CCxxCC", 65, 66, 67, 68); # $out 等于 "AB\0\0CD" 打包你的短整数并不意味着你就可移植了: $out = pack("s2", 1, 2); # 在小头在前的机器上是 "\1\0\2\0" # 在大头在前的机器上是 "\0\1\0\2" 在二进制和十六进制包装上,count 指的是位或者半字节的数量,而不是生成的字节数量: $out = pack("B32", "..."); $out = pack("H8", "5065726c"); # 都生成“Perl” a 域里的长度只应用于一个字串: $out = pack("a4", "abcd", "x", "y", "z"); # "abcd" 要绕开这个限制,使用多倍声明: $out = pack("aaaa", "abcd", "x", "y", "z"); # "axyz" $out = pack("a" x 4, "abcd", "x", "y", "z aa8d "); # "axyz" a 格式做空填充: $out = pack("a14", "abcdefg"); # " abcdefg\0\0\0\0\0\0"
关于unpack: array unpack ( string $format, string $data ) $data = "010000020007"; unpack("Sint1/Cchar1/Sint2/Cchar2",$data); # array('int1'=>1, 'char1'=>'0','int2'=>2,'char2'=>7);
举例 协议格式如下: 包头(2bytes)+加密(1byte)+命令码(2bytes)+帧内容 1.包头的内容是记录帧内容的长度; 2.加密:0表示不加密,1表示加密; 3.命令码为服务端命令识别符号; 使用pack/unpack 举例程序代码为 : $lastact = pack('SCSa32a32',0x0040, 0x00, 0x0006, $username, $passwd ); unpack('Sint1/Cchar1/Sint2/Cchar2/',$lastmessage);
感谢分享 参考资料
http://my.oschina.net/goal/blog/195749
http://www.03sec.com/3061.shtml
http://blog.sina.com.cn/s/blog_3eba8f1c0100nq9r.html
http://bbs.phpchina.com/thread-104492-1-1.html
http://www.neatstudio.com/show-2327-1.shtml
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- mysql binlog二进制日志详解
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- 详解C++编程中对二进制文件的读写操作