PHP unpack() 函數(shù)
PHP Misc 參考手冊(cè)
實(shí)例
從二進(jìn)制字符串對(duì)數(shù)據(jù)進(jìn)行解包:
<?php
$data = "PHP";
print_r(unpack("C*",$data));
?>
運(yùn)行實(shí)例 ?
定義和用法
unpack() 函數(shù)從二進(jìn)制字符串對(duì)數(shù)據(jù)進(jìn)行解包。
語法
參數(shù) | 描述 |
format | 必需。規(guī)定在解包數(shù)據(jù)時(shí)所使用的格式。 可能的值: - a - NUL 填充的字符串
- A - SPACE 填充的字符串
- h - 十六進(jìn)制字符串,低位在前
- H - 十六進(jìn)制字符串,高位在前
- c - signed char
- C - unsigned char
- s - signed short(總是16位, machine 字節(jié)順序)
- S - unsigned short(總是16位, machine 字節(jié)順序)
- n - unsigned short(總是16位, big endian 字節(jié)順序)
- v - unsigned short(總是16位, little endian 字節(jié)順序)
- i - signed integer(取決于 machine 的大小和字節(jié)順序)
- I - unsigned integer(取決于 machine 的大小和字節(jié)順序)
- l - signed long(總是32位, machine 字節(jié)順序)
- L - unsigned long(總是32位, machine 字節(jié)順序)
- N - unsigned long(總是32位, big endian 字節(jié)順序)
- V - unsigned long(總是32位, little endian 字節(jié)順序)
- f - float(取決于 machine 的大小和表示)
- d - double(取決于 machine 的大小和表示)
- x - NUL 字節(jié)
- X - 備份一個(gè)字節(jié)
- Z - NUL 填充的字符串
- @ - NUL 填充絕對(duì)位置
|
data | 必需。規(guī)定被解包的二進(jìn)制數(shù)據(jù)。 |
技術(shù)細(xì)節(jié)
返回值: | 如果成功則返回?cái)?shù)組,如果失敗則返回 FALSE。 |
PHP 版本: | 4+ |
更新日志: | 自 PHP 5.5.0 起,為 Perl 兼容進(jìn)行了下列更改:
"a" 代碼保留尾隨 NULL 字節(jié)。 "A" 代碼刪除所有尾隨 ASCII 空白。 新增 "Z" 代碼用于 NUL 填充的字符串,并移除尾隨 NULL 字節(jié)。 |
更多實(shí)例
實(shí)例 1
對(duì)數(shù)據(jù)進(jìn)行解包:
<?php
$data = "PHP";
print_r(unpack("C*myint",$data));
?>
運(yùn)行實(shí)例 ? 實(shí)例 2
對(duì)數(shù)據(jù)進(jìn)行解包:
<?php
$bin = pack("c2n2",0x1234,0x5678,65,66);
print_r(unpack("c2chars/n2int",$bin));
?>
運(yùn)行實(shí)例 ?
PHP Misc 參考手冊(cè)
更多建議: