from typing import Sequence _DIGITS = "0123456789abcdef" def hex_encode(bytes: Sequence[int]) -> str: """Bytes as lowercase hexadecimal, two digits per byte. Bytes are a list of integers 0-255 across the registry (there is no bytes type); a Python ``bytes`` value is such a sequence and is accepted as is. A value outside the range is refused rather than masked to a byte. """ if isinstance(bytes, (str, dict)) or not hasattr(bytes, "__len__"): raise TypeError("bytes must be a list of integers from 0 to 255") out = [] for b in bytes: # bool is an int in Python; true is not a byte in the other two languages. if type(b) is not int or b < 0 or b > 255: raise ValueError("bytes must be a list of integers from 0 to 255") out.append(_DIGITS[b >> 4] + _DIGITS[b & 15]) return "".join(out)