开发者

output raw binary integer in php

In PHP, I have some integer variables with values from 0-65535. I need to echo/print it directly, NOT as a printed sequence of characters like printing the string "1281" but the raw binary value. Also I need it so the binary integer sent to the output will always be exactly 2 bytes (zero bytes sent i开发者_如何学Gof needed so that it is always 2 bytes). How do I do this in php?


To elaborate on mu's answer, you want:

pack("S", $num));

E.g.:

file_put_contents("test65535.bin", pack("S", 65535));
file_put_contents("test100.bin", pack("S", 100));
file_put_contents("test0.bin", pack("S", 0));

hexdump test65535.bin
0000000 ffff
0000002

hexdump test100.bin
0000000 0064
0000002

hexdump test0.bin
0000000 0000
0000002


Yes it is possible, use pack.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜