'Incremental encode, keep track of digits and emit a byte when a pair
of hex digits is found. The space is optional unless the error
handling is defined to be \'strict\'.'

def encode(self, data, final=False):

    state = self.state
    encoded = []
    for c in data.upper():
        if (c in HEXDIGITS):
            z = HEXDIGITS.index(c)
            if state:
                encoded.append((z + (state & 240)))
                state = 0
            else:
                state = (256 + (z << 4))
        elif (c == '    '):
            if (state and (self.errors == 'strict')):
                raise UnicodeError('odd    number    of    hex    digits')
            state = 0
        elif (self.errors == 'strict'):
            raise UnicodeError('non-hex    digit    found:    {!r}'.format(c))
    self.state = state
    return serial.to_bytes(encoded)
