From 55ef8cd3805ab6082d2edf39a25f4d18a97864df Mon Sep 17 00:00:00 2001 From: Nicolas Jouanin Date: Sun, 31 May 2015 14:12:18 +0200 Subject: [PATCH] Compute bit_length --- hbmqtt/codecs/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hbmqtt/codecs/utils.py b/hbmqtt/codecs/utils.py index 2c7256b..bbbb678 100644 --- a/hbmqtt/codecs/utils.py +++ b/hbmqtt/codecs/utils.py @@ -4,6 +4,7 @@ import asyncio from asyncio import IncompleteReadError from hbmqtt.codecs.errors import NoDataException +from math import ceil def bytes_to_hex_str(data): """ @@ -21,13 +22,16 @@ def bytes_to_int(data): """ return int.from_bytes(data, byteorder='big') -def int_to_bytes(int_value:int) -> bytes: +def int_to_bytes(int_value: int) -> bytes: """ convert an integer to a sequence of bytes using big endian byte ordering :param int_value: integer value to convert :return: byte sequence """ - int_value.to_bytes(int_value.bit_length(), byteorder='big') + byte_length = ceil(int_value.bit_length()/8) + if byte_length == 0: + byte_length = 1 + return int_value.to_bytes(byte_length, byteorder='big') @asyncio.coroutine