如何發(fā)送接收16進(jìn)制數(shù)據(jù)
接收16進(jìn)制數(shù)據(jù)
當(dāng)收到數(shù)據(jù)后用函數(shù)bin2hex($data)
可以將數(shù)據(jù)轉(zhuǎn)換成16進(jìn)制。
發(fā)送16進(jìn)制數(shù)據(jù)
發(fā)送數(shù)據(jù)前用hex2bin($data)
將16進(jìn)制數(shù)據(jù)轉(zhuǎn)換成二進(jìn)制發(fā)送。
示例:
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('text://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, $data){
// 得到16進(jìn)制數(shù)據(jù)
$hex_data = bin2hex($data);
// 向客戶端發(fā)送16進(jìn)制數(shù)據(jù)
$connection->send(hex2bin($hex_data));
};
Worker::runAll();