def from_hex(arg: str, length: int) -> bytes:
    """
    Transform a hexadecimal string into a bytearray of a given length.

    :return: a bytearray
    """
    narg = int(arg, 16)
    if length == 0:
        length = 512
    barg = narg.to_bytes(int(length/2), 'big')
    return barg