def str2int(val):
    """
    Converts a decimal or hex string into an int.
    Also accepts an int and returns it unchanged.
    """
    if type(val) is int:
        return int(val)
    if len(val) < 3:
        return int(val, 10)
    if val[:2] == '0x':
        return int(val, 16)
    return int(val, 10)