Skip to content

Instantly share code, notes, and snippets.

@koi8-r
Last active September 4, 2025 15:39
Show Gist options
  • Select an option

  • Save koi8-r/8929e6090af55a5325d078dcdf6b1431 to your computer and use it in GitHub Desktop.

Select an option

Save koi8-r/8929e6090af55a5325d078dcdf6b1431 to your computer and use it in GitHub Desktop.
from typing import TypeAlias, TypeVar
T = TypeVar("T")
Tuple6: TypeAlias = tuple[T, T, T, T, T, T]
OCTET_MAX = 0xFF
def parse(octets: Tuple6[str]) -> int: # like: int(str, 16)
off = ret = 0
for ch in reversed(octets):
try:
o = int(ch, 16)
if not 0 <= o <= OCTET_MAX:
raise ValueError
except ValueError:
raise ValueError("not a valid hex octet", ch) from None
ret = ret | (o << off)
off += 4 * len(ch)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment