* Thus wrote Ren Fournier ([EMAIL PROTECTED]): > When reading from a Socket Server, my Socket Client retrieves data that > includes ASCII and binary data: > > while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) { > $data .= $buf; > if(preg_match("/ENX/", $data)) break; > } > echo "RESPONSE: ".$data."\n"; > > The output looks like: > RESPONSE: STX&The system is up and running.ENX > > Between the "STX" and "The system is up..." are 8 bytes of binary data > (which this case appear as an ampersand), which I want to extract from > $data, split into two 4 byte variables, and convert back into integers. > I suspect that I need unpack() at some point, but any ideas how to > extract and split the binary data from this string? (Sometimes, the > binary data is not visible, but it's still there.)
//I would Assume that that was packed with something like this: $string = 'The system is up and running.'; $pck = pack('a3iia*a3', 'STX', strlen($string), 38, $string, 'ENX'); echo $pck, "\n"; //So This will unpack it for you. $unpacked = unpack('a3code/istrlen/itype/a*stringWithEndDelim', $pck); print_r($unpacked); //You'll notice the stringWithCode will have to be adjusted. $stringResult = substr($unpacked['stringWithEndDelim'], 0, -3); $stringEndDelim = substr($unpacked['stringWithEndDelim'], -3); echo $stringResult, "\n" ; echo $stringEndDelim, "\n" ; // and check the string to make sure data isn't currupt: if (strlen($stringResult) != $unpacked['strlen']) { echo 'corrupt', "\n"; } else { echo 'fine', "\n"; } Curt -- "I used to think I was indecisive, but now I'm not so sure." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php