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: