c# - How do I use the PHP Pack function to pack an integer into a byte array? -
c# - How do I use the PHP Pack function to pack an integer into a byte array? -
the next c# code used in service have inherited, convert little integer 3 bytes can sent on socket.
int = 12345; byte[] info = new byte[] { (byte)i, (byte)(i >> 8), (byte)(i >> 16)}; var result = data[0] + (data[1] << 8) + (data[2] << 16); using php opening socket service. communications protocol written @ byte level, have send bytes on php socket.
i have determined this, have utilize pack function.
<?php $binarydata = pack("cccc", 0xff, 0x00, 0x00, 0x00); socket_write($sk, $binarydata, $binarydatalen); ?> the first byte tells server client wants do, , in case next 3 bytes must represent integer conforming formulae shown in c# implementation above.
the problem have, no matter try, cannot create 3 byte array integer matches c# implementation.
i appreachiate , vote 1 can help me solve this, experienced php developer think might know right syntax use.
thank you. christian
edit: here prototype
//the client error_reporting(e_all); $address = "127.0.0.1"; $port = <removed>; /* create tcp/ip socket. */ $socket = socket_create(af_inet, sock_stream, sol_tcp); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "socket created.\n"; } echo "attempting connect '$address' on port '$port'..."; $result = socket_connect($socket, $address, $port); if ($result === false) { echo "socket_connect() failed.\nreason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else { echo "successfully connected $address.\n"; } $value = 17; $binarydata = pack("c*", 0xfe, $value & 0x000f, ($value>>8) & 0x000f, ($value>>16) & 0x000f); socket_write($socket, $binarydata, 4); $input = socket_read($socket, 2048); echo "response server is: $input\n"; sleep(5); echo "closing socket..."; socket_close($socket); the expected output "fe 11 00 00" (11 beingness 17)
unfortunately sending "fe 01 00 00" according logs :-(
the server responds in ascii need number right alter dynamically.
edit: all, working! xd
$binarydata = pack("cc*", 0xfe, $value, ($value>>8), ($value>>16));
the format "nvc*" not correct. generate 6 bytes. should this:
$binarydata = pack("c*", $value & 0x000f, ($value>>8) & 0x000f, ($value>>16) & 0x000f); c# php .net bytearray pack
Comments
Post a Comment