开发者

Echoing PHP pack() result... Normal?

Thi开发者_如何学Pythons code:

<?php

$string = "I love chicken.";
$binary = pack("a15", $string);
echo $binary;

?>

Outputs "I love chicken". Is that normal? Shouldn't it output some binar-ish gibberish?


Yes, that's normal. You're packing a 15-character string into a 15 byte NULL padded string, so there's no "gibberish" (because your original string is stored in memory "that way".) You'd see gibberish if, for example you tried to pack integers, etc.


Why? The "binary" representation of a string (in a single-byte charset) is exactly that string, so there is no need to convert anything in this case.


Not if you're packing an ASCII string as an ASCII string of the same length. If you change a15 to a16, then pack will pad the output with nulls, which aren't visible if you echo, but are if you do var_dump()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜