Home  >  Article  >  Backend Development  >  PHP的pack unpcak该怎么用

PHP的pack unpcak该怎么用

WBOY
WBOYOriginal
2016-06-23 14:09:11991browse

server端是C的, 小端模式
现在比如我要打包:
$ps = pack("ia5",10,"hello");
打包成这样, server端能认出来么?

当我解包的时候
$a = unpack("ia5", $ps);
print_r($a);
变成了:
Array
(
    [a5] => 10
)
输出的不对呀, 这个该怎么解包呢?

我想按照我指定的格式来pack和unpack, 该怎么打包数值和字符串一起的那种呢?


回复讨论(解决方案)

$ps = pack("ia5",10,"hello");$a = unpack("in/a5s", $ps);print_r($a);
Array
(
    [n] => 10
    [s] => hello
)

多谢多谢, 不过 "in/a5s" 必须要有这个n和s么? 我刚刚试了下i/a5
却输出了
Array
(
    [1] => hello
)

unpack 有点小 bug
如果解包的目标不是相同的数据类型,则需要命名。否则会丢失部分数据
不过命名后,对访问也带来不少便利,至少不需要再数数了

那除了这种方法还有什么其他的方法么 多谢

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn