我有一个WebSocket
编码器:
print_r(frameEncode("How can I convert a byteArray to String in PHP?"));
function frameEncode($message) {
$messageBytes = array();
$messageLength = strlen($message);
$messageBytes[0] = 129;
if ($messageLength < 126) {
$messageBytes[1] = $messageLength;
} else if ($messageLength <= 65535) {
$messageBytes[1] = 126;
$messageBytes[2] = ($messageLength >> 8) & 255;
$messageBytes[3] = $messageLength & 255;
} else {
$messageBytes[1] = 127;
$messageBytes[2] = ($messageLength >> 56) & 255;
$messageBytes[3] = ($messageLength >> 48) & 255;
$messageBytes[4] = ($messageLength >> 40) & 255;
$messageBytes[5] = ($messageLength >> 32) & 255;
$messageBytes[6] = ($messageLength >> 24) & 255;
$messageBytes[7] = ($messageLength >> 16) & 255;
$messageBytes[8] = ($messageLength >> 8) & 255;
$messageBytes[9] = $messageLength & 255;
}
return pack("C*", $messageBytes) . $message;
}
如何在函数末尾将$messageBytes
转换为字符串?数组值只是被pack()
忽略了。
您可以像这样使用call_user_func_array:
call_user_func_array('pack', array_merge(array('C*'), $messageBytes)))
return pack("C*", implode(' ', $messageBytes)) . $message;