示例#1
0
def long_to_bytearray(num):
    """
    Converts a long into a byte array.

    :param num:
        Long value
    :returns:
        Long.
    """
    bytes_count = byte_count(num)
    byte_array = bytearray_create_zeros(bytes_count)
    for count in range(bytes_count - 1, -1, -1):
        byte_array[count] = int(num % 256)
        num >>= 8
    return byte_array
示例#2
0
def generate_random_long(low, high):
    """
    Generates a random long integer.

    :param low:
        Low
    :param high:
        High
    :returns:
        Random long integer value.
    """
    if low >= high:
        raise ValueError("High must be greater than low.")
    num_bits = bit_count(high)
    num_bytes = byte_count(high)
    last_bits = num_bits % 8
    while 1:
        byte_array = generate_random_bytearray(num_bytes)
        if last_bits:
            byte_array[0] = byte_array[0] % (1 << last_bits)
        n = bytearray_to_long(byte_array)
        if n >= low and n < high:
            return n