From 67806a76b84e4f1597ab9e438675b47e4122abb6 Mon Sep 17 00:00:00 2001 From: Arjan Zijderveld <5286904+arjanz@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:32:33 +0100 Subject: [PATCH] Fixed incorrect encoding byte length --- jamcodec/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jamcodec/types.py b/jamcodec/types.py index bd73348..3987aa2 100644 --- a/jamcodec/types.py +++ b/jamcodec/types.py @@ -831,6 +831,8 @@ def __init__(self, length: int, strict_decoding: bool = True): def encode(self, value: Union[list, str, bytes]) -> JamBytes: + byte_length = (self.length + 7) // 8 + if type(value) is list: value = sum(v << i for i, v in enumerate(value)) @@ -847,8 +849,6 @@ def encode(self, value: Union[list, str, bytes]) -> JamBytes: if type(value) is not int: raise ScaleEncodeException("Provided value is not an int, binary str or a list of booleans") - byte_length = math.ceil(value.bit_length() / 8) - return JamBytes(value.to_bytes(length=byte_length, byteorder='little')) def decode(self, data: JamBytes) -> list: