def encode(self):
        """
        Return binary string representation of object.
        
        :rtype: str
        """
        buf = bytearray()
        for typ in sorted(self.format.keys()):
            encoded = None
            if typ != 0xFFFF: # end of block
                (name, marshall) = self.format[typ]
                value = getattr(self, name, None)
                if value is not None:
                    try:
                        encoded = marshall.encode(value)
                        self.log.debug("Encoded field [{0}] to value {1!r}".format(name, encoded))
                    except:
                        self.log.exception("Error encoding key/value: key={0}, value={1!r}".format(name, value))
                        raise
            
            # Note, there is an assumption here that encode() func is returning
            # a byte string (so len = num bytes).  That should be a safe assumption.
            size = len(encoded) if encoded is not None else 0
            packed = struct.pack('<H', typ)
            packed += struct.pack('<I', size)
            if encoded is not None:
                if isinstance(encoded, bytearray):
                    encoded = str(encoded)
                elif isinstance(encoded, unicode):
                    encoded = encoded.encode('utf-8')
                packed += struct.pack('<%ds' % size, encoded)
                
            buf += packed
            
        return buf
Return binary string representation of object.
        
        :rtype: str