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.
精彩评论