PHPDaemon\Utils\Binary::bitmap2bytes PHP Method

bitmap2bytes() public static method

Convert bitmap into bytes
public static bitmap2bytes ( string $bitmap, integer $check_len ) : string | false
$bitmap string Bitmap
$check_len integer Check length?
return string | false
    public static function bitmap2bytes($bitmap, $check_len = 0)
    {
        $r = '';
        $bitmap = str_pad($bitmap, ceil(mb_orig_strlen($bitmap) / 8) * 8, '0', STR_PAD_LEFT);
        for ($i = 0, $n = mb_orig_strlen($bitmap) / 8; $i < $n; ++$i) {
            $r .= chr((int) bindec(mb_orig_substr($bitmap, $i * 8, 8)));
        }
        if ($check_len && mb_orig_strlen($r) !== $check_len) {
            return false;
        }
        return $r;
    }

Usage Example

Beispiel #1
0
 /**
  * Gets the host information
  * @param  string   $hostname Hostname
  * @param  callable $cb       Callback
  * @callback $cb ( )
  * @return void
  */
 public function get($hostname, $cb)
 {
     $this->onResponse->push($cb);
     $this->setFree(false);
     $e = explode(':', $hostname, 3);
     $hostname = $e[0];
     $qtype = isset($e[1]) ? $e[1] : 'A';
     $qclass = isset($e[2]) ? $e[2] : 'IN';
     $QD = [];
     $qtypeInt = array_search($qtype, Pool::$type, true);
     $qclassInt = array_search($qclass, Pool::$class, true);
     if ($qtypeInt === false || $qclassInt === false) {
         call_user_func($cb, false);
         return;
     }
     $q = Binary::labels($hostname) . Binary::word($qtypeInt) . Binary::word($qclassInt);
     $QD[] = $q;
     $packet = Binary::word(++$this->seq) . Binary::bitmap2bytes('0' . '0000' . '0' . '0' . '1' . '0' . '000' . '0000', 2) . Binary::word(sizeof($QD)) . Binary::word(0) . Binary::word(0) . Binary::word(0) . implode('', $QD);
     if ($this->type === 'udp') {
         $this->write($packet);
     } else {
         $this->write(Binary::word(strlen($packet)) . $packet);
     }
 }