def get_dtype_str(dtype, byteorder="little"):
    """Parses dtype and byte order to return a type string code.

    Parameters
    ----------
    dtype : `numpy.dtype` or `str`
        Either a dtype (e.g., ``numpy.uint32``) or a string with the type code
        (``'>u4'``). If a string type code and the first character indicates
        the byte order (``'>'`` for big, ``'<'`` for little endian),
        ``byteorder`` will be ignored. The type code refers to bytes, while the
        dtype classes refer to bits, i.e., ``'u2'`` is equivalent to
    byteorder : str
        Either ``'big'`` for big endian representation or ``'little'`` for
        little end. ``'>'`` and ``'<'`` are also accepted, respectively.

    Returns
    -------
    type_code : `str`
        The type code for the input dtype and byte order.

    Examples
    --------
    ::

        >>> get_dtype_str(numpy.uint32, byteorder='big')
        '>u4'
        >>> get_dtype_str('u2', byteorder='>')
        '>u2'
        >>> get_dtype_str('<u2', byteorder='big')
        '<u2'

    """

    if byteorder == "big":
        byteorder = ">"
    elif byteorder == "little":
        byteorder = "<"
    elif byteorder in [">", "<"]:
        pass
    else:
        raise ValueError(f"invalid byteorder {byteorder}")

    if isinstance(dtype, str):
        if dtype[0] in [">", "<"]:
            return dtype
        elif dtype[0] == "=":
            raise ValueError("invalid byte order =. Please, use a specific endianess.")
        else:
            return byteorder + dtype

    dtype_str = dtype().dtype.str

    return byteorder + dtype_str[1:]