public function toBinary()
    {
        $this->uniqueTable = array();
        $this->countObjects = 0;
        $this->stringSize = 0;
        $this->intSize = 0;
        $this->miscSize = 0;
        $this->objectRefs = 0;

        $this->writtenObjectCount = 0;
        $this->objectTable = array();
        $this->objectRefSize = 0;

        $this->offsets = array();

        $binary_str = "bplist00";
        $value = $this->getValue(true);
        $this->uniqueAndCountValues($value);

        $this->countObjects += count($this->uniqueTable);
        $this->objectRefSize = self::bytesNeeded($this->countObjects);
        $file_size = $this->stringSize + $this->intSize + $this->miscSize + $this->objectRefs * $this->objectRefSize + 40;
        $offset_size = self::bytesNeeded($file_size);
        $table_offset = $file_size - 32;

        $this->objectTable = array();
        $this->writtenObjectCount = 0;
        $this->uniqueTable = array(); // we needed it to calculate several values
        $value->toBinary($this);

        $object_offset = 8;
        $offsets = array();

        for ($i=0; $i<count($this->objectTable); ++$i) {
            $binary_str .= $this->objectTable[$i];
            $offsets[$i] = $object_offset;
            $object_offset += strlen($this->objectTable[$i]);
        }

        for ($i=0; $i<count($offsets); ++$i) {
            $binary_str .= self::packItWithSize($offset_size, $offsets[$i]);
        }


        $binary_str .= pack("x6CC", $offset_size, $this->objectRefSize);
        $binary_str .= pack("x4N", $this->countObjects);
        $binary_str .= pack("x4N", 0);
        $binary_str .= pack("x4N", $table_offset);

        return $binary_str;
    }