def str_to_bytes(s: str, n: int) ->bytes:
    """Converts a string into a bytes object containing the string, null-terminated, and truncated to n - 1 characters"""
    b = b''
    for i in range(n - 1):
        try:
            b += s[i].encode('ASCII')
        except IndexError:
            b += b'\x00'
    b += b'\x00'
    return b
