Tuesday, 15 March 2011

PHP convert integer to 32 bit (4 Byte) hex for socket programming -



PHP convert integer to 32 bit (4 Byte) hex for socket programming -

i need convert integer 4 byte (32 bit) hex sending ack device trying integrate.

for example

3 = 00000003 15 = 0000000f

check http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html 1. select signed 32 bit dropdown 2. come in value in decomal text box 3. check value in hex field.

i using php pack function parameter based on response device, not seem right approach.

$reply = pack(l*,$num);

is right parameter or there other way. please suuggest.

i do

$a = 15; var_dump( sprintf("%08x", $a) ); $a = 3; var_dump( sprintf("%08x", $a) );

this outputs

string(8) "0000000f" string(8) "00000003

08x means create 8 char string padded 0 (if needed) argument beingness treated hex. (upper case letters)

so in illustration

$reply = sprintf("%08x", $num)

php sockets byte pack unpack

No comments:

Post a Comment