PHPDaemon\Utils\Binary::bytes2int PHP Method

bytes2int() public static method

Convert bytes into integer
public static bytes2int ( string $str, boolean $l = false ) : integer
$str string Bytes
$l boolean Little endian? Default is false
return integer
    public static function bytes2int($str, $l = false)
    {
        if ($l) {
            $str = strrev($str);
        }
        $dec = 0;
        $len = mb_orig_strlen($str);
        for ($i = 0; $i < $len; ++$i) {
            $dec += ord(mb_orig_substr($str, $i, 1)) * pow(0x100, $len - $i - 1);
        }
        return $dec;
    }

Usage Example

コード例 #1
0
ファイル: Connection.php プロジェクト: kakserpom/phpdaemon
 /**
  * Called when new data received
  * @return void
  */
 public function onRead()
 {
     start:
     if ($this->type === 'udp') {
         $this->onUdpPacket($this->read($this->getInputLength()));
         return;
     }
     if ($this->state === self::STATE_ROOT) {
         if (false === ($hdr = $this->readExact(2))) {
             return;
             // not enough data
         }
         $this->pctSize = Binary::bytes2int($hdr);
         $this->setWatermark($this->pctSize);
         $this->state = self::STATE_PACKET;
     }
     if ($this->state === self::STATE_PACKET) {
         if (false === ($pct = $this->readExact($this->pctSize))) {
             return;
             // not enough data
         }
         $this->state = self::STATE_ROOT;
         $this->setWatermark(2);
         $this->onUdpPacket($pct);
     }
     goto start;
 }
All Usage Examples Of PHPDaemon\Utils\Binary::bytes2int