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
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*", 0x1234, 0x5678, 65, 66);
?>
The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
语法:unpack(format,args+)
参数 描述
format 必需。规定在包装数据时所使用的格式。
args+ 可选。规定被包装的一个或多个参数。
参数与pack相同。
ord函数(ord函数:为序数函数,函数返回值为字符在ASCII码中的序号。)说明:返回对应字符的acill码值
语法:ord($character);
//A字符 $str = (pack ( "A*", "中国" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //H字符 $str = (pack ( "H*", "fffe" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //C字符 $str = (pack ( "C*", "55", "56", "57" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //i字符 短整形 32位 4个字节 64位8个字节 $str = (pack ( "i", "100" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //s字符 短整形 2个字节 $str = (pack ( "s", "100" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //l字符 长整形 4个字节 $str = (pack ( "l", "100" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //f字符 单精度浮点 4个字节 $str = (pack ( "f", "100" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; //d字符 双精度浮点 8个字节 $str = (pack ( "d", "100" )); echo $str, "=", strlen ( $str ), "字节 "; getAscill ( $str ); echo '<br/>'; function getAscill($str) { $arr = str_split ( $str ); foreach ( $arr as $v ) { echo $v, "=", ord ( $v ), " "; } echo "============= "; }
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字节,字符串
各字段间用:" "分割
A、PACK封包
$code=array( "username"=>array("A20","张三adfb12"), "pass"=>array("A10","asdf*#1"), "age"=>array("C","23"), "birthday"=>array("I","19900101"), "email"=>array("A50","zhangsan@163.com")); $stream=join(" ",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)," "; } }
(在浏览器中用utf8编码查看:张三adfb12 asdf*#1 Ŧ/zhangsan@163.com 89; 文件内容:
张三adfb12 asdf*#1 Ŧ/ zhangsan@163.com )
因为用”