diff --git a/tivars/data.py b/tivars/data.py index e9a2699..ef1dd9b 100644 --- a/tivars/data.py +++ b/tivars/data.py @@ -202,7 +202,12 @@ def set(cls, value: _T, *, length: int = None, **kwargs) -> bytes: :return: The little-endian representation of ``value`` """ - return int.to_bytes(value, length if length is not None else 2, 'little') + length = length if length is not None else 2 + try: + return int.to_bytes(value, length, 'little') + + except OverflowError: + raise OverflowError(f"{value} cannot fit in a field of width {length}") class String(Converter):