🛠️ToolsShed

Testo in Esadecimale

Converti testo in codifica esadecimale e viceversa.

Separatore:

Domande Frequenti

Implementazione del Codice

def text_to_hex(text: str, separator: str = " ") -> str:
    """Convert UTF-8 text to hexadecimal string."""
    encoded = text.encode("utf-8")
    hex_parts = [f"{byte:02x}" for byte in encoded]
    return separator.join(hex_parts)

def hex_to_text(hex_string: str) -> str:
    """Convert hexadecimal string back to UTF-8 text."""
    # Remove common separators
    cleaned = hex_string.replace(" ", "").replace("-", "")
    if len(cleaned) % 2 != 0:
        raise ValueError("Hex string must have an even number of characters")
    byte_values = bytes(int(cleaned[i:i+2], 16) for i in range(0, len(cleaned), 2))
    return byte_values.decode("utf-8")

# Examples
print(text_to_hex("Hello"))       # 48 65 6c 6c 6f
print(text_to_hex("Hi", "-"))     # 48-69
print(hex_to_text("48 65 6c 6c 6f"))  # Hello
print(hex_to_text("48656c6c6f"))       # Hello

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.