Datasets:
Download runtime/stdlib/hex.py from SNAPKITTYWEST/mantra: direct link, hf CLI and curl.
- Browser
- Download file 443 Bytes
-
https://huggingface.co/datasets/SNAPKITTYWEST/mantra/resolve/main/runtime/stdlib/hex.py
- Command line
-
hf download hf://datasets/SNAPKITTYWEST/mantra/runtime/stdlib/hex.py
-
curl -L -o hex.py https://huggingface.co/datasets/SNAPKITTYWEST/mantra/resolve/main/runtime/stdlib/hex.py
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") | |