mantra / runtime /stdlib /hex.py
SNAPKITTYWEST's picture
Upload runtime/stdlib/hex.py
603f13c verified
Raw
History Blame Contribute Delete
443 Bytes
from runtime.types import Bytes, String, InvalidHex
def bytes_to_hex(b: Bytes) -> String:
return String(b.data.hex())
def hex_to_bytes(s) -> Bytes:
text = s.text if isinstance(s, String) else str(s)
text = text.removeprefix('0x')
if len(text) % 2 != 0:
raise InvalidHex("hex length must be even")
try:
return Bytes(bytes.fromhex(text))
except Exception:
raise InvalidHex("invalid hex string")