1. 程式人生 > >MQTT中的Remaining Length計算方式(php版)

MQTT中的Remaining Length計算方式(php版)

MQTT中的Remaining Length,最大長度為4個位元組。
其中每個位元組的第一位為 “是否有後續位元組” 的狀態位。
如果有設定該狀態位為1,沒有則設定為0。

估可用小於或等於4個位元組來表示 MQTT包的長度(該4位不計入在包大小範圍,因此忽略計入)。

<?php
function numerical2Binary($number)
{
    $char = [];

    $first = 0;
    $next  = $number;
    $key   = 0;
    do {
        $first = $next % 128;
        $next
= intval($next / 128); $char[$key] = $first; if ($next > 0) { $char[$key] |= 0x80; } ++$key; } while($next > 0); return $char; } $result = numerical2Binary(321); foreach ($result as $key => $value) { printf("%08b \n", $value); }