def parse_varint(tx):
    """ Parses a given transaction for extracting an encoded varint element.

    :param tx: Transaction where the element will be extracted.
    :type tx: TX
    :return: The b-bytes representation of the given value (a) in hex format.
    :rtype: hex str
    """
    data = tx.hex[tx.offset:]
    assert len(data) > 0
    size = int(data[:2], 16)
    assert size <= 255
    if size <= 252:
        storage_length = 1
    elif size == 253:
        storage_length = 3
    elif size == 254:
        storage_length = 5
    elif size == 255:
        storage_length = 9
    else:
        raise Exception('Wrong input data size')
    varint = data[:storage_length * 2]
    tx.offset += storage_length * 2
    return varint
