def hex_to_int(hex_string, bits=16):
    """Convert a four-character hex string to an integer.

    This uses two's complement in decoding the hex string, per Huber's
    manual. This also manages the value '7fff' (32767), which is
    Huber's version of a command-not-implemented error.
    """
    number = int(hex_string, bits)
    if number == 32767:
        raise IOError('Command not enabled on this Huber model.')
    if number >> bits - 1 == 1:
        number -= 1 << bits
    return number
