import struct


def itoby(v, length):
    """
    Converts an unsigned integer to a bytearray of specified length.
    """
    fmt = {1: "<B", 2: "<H", 4: "<L", 8: "<Q"}[length]
    return bytearray(struct.pack(fmt, v))