Hexadecimal text (Effect's encoding/Hex): encode writes lowercase,
decode reads either case and returns a Result, random makes
random lowercase hex (not cryptographically secure: use Crypto for
secrets).
Functions#
decode_string#
decode_string(text) -> Result[str, EncodingError]decode, then the bytes as UTF-8 (bad bytes replaced, a leading U+FEFF kept).
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns Result[str, EncodingError]
def decode_string(text: str) -> Result[str, EncodingError]:
"""``decode``, then the bytes as UTF-8 (bad bytes replaced, a leading U+FEFF kept)."""
return decoded_text(decode(text))random#
random(length) -> strlength random hex digits, rounded down to a multiple of 8;
length is taken modulo 2**32 as Effect's length >>> 3.
| Parameter | Type | Description |
|---|---|---|
length | int |
Returns str
def random(length: int) -> str:
"""``length`` random hex digits, rounded down to a multiple of 8;
``length`` is taken modulo ``2**32`` as Effect's ``length >>> 3``."""
words = (length % 2**32) >> 3
return "".join(f"{random_.getrandbits(32):08x}" for _ in range(words))hex_decode#
hex_decode(text) -> Result[bytes, EncodingError]Two hex digits, either case, per byte.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns Result[bytes, EncodingError]
def hex_decode(text: str) -> Result[bytes, EncodingError]:
"""Two hex digits, either case, per byte."""
data = text_encode(text)
if len(data) % 2:
return _fail("Hex", text, f"Length must be a multiple of 2, but is {len(data)}")
if not _HEX.fullmatch(data):
return _fail("Hex", text, "Invalid input")
return Ok(bytes.fromhex(data.decode("ascii")))hex_encode#
hex_encode(value) -> str| Parameter | Type | Description |
|---|---|---|
value | Binary |
Returns str
def hex_encode(value: Binary) -> str:
return _bytes(value).hex()