Files
coin-counter-simulators/source/common.py
2025-11-10 09:11:56 +01:00

21 lines
507 B
Python

from typing import Literal
def format_comm_debug(
prefix: Literal["RX", "TX"], data: bytes, include_ascii: bool = False
) -> str:
hex_representation = " ".join(f"{byte:02X}" for byte in data)
output = [
f"{prefix}: ",
hex_representation,
]
if include_ascii:
ascii_representation = "".join(
(chr(byte) if 32 <= byte <= 126 else ".") for byte in data
)
output.append(" // ASCII: " + ascii_representation)
return "".join(output)