from typing import ByteString
from typing import Optional


def hexdump_line(_bytes: ByteString, length: Optional[int] = None) -> str:
    """Create a hexdump formatted line for supplied bytes.

    If length is greater than len(_bytes), the line will be padded with ASCII
    space characters to indicate no byte data is present.

    Arguments:
        _bytes: The bytes to format.

        length (optional): The optional length of the output line. This should be
            greater than the length of bytes if provided.

    Returns:
        The hexdump formatted line.
    """
    line = ""

    if length is None:
        length = len(_bytes)

    for n in range(0, length, 2):
        if n < len(_bytes) - 1:
            line += "%02x%02x  " % (_bytes[n], _bytes[n + 1])
        elif n < len(_bytes):
            line += "%02x    " % _bytes[n]
        else:
            line += "      "

    line += "*"

    for n in range(length):
        if n < len(_bytes):
            if _bytes[n] in range(32, 127):
                line += "%c" % _bytes[n]
            else:
                line += "."
        else:
            line += " "

    line += "*"
    return line