Compute bit_length

pull/8/head
Nicolas Jouanin 2015-05-31 14:12:18 +02:00
rodzic 5708922398
commit 55ef8cd380
1 zmienionych plików z 6 dodań i 2 usunięć

Wyświetl plik

@ -4,6 +4,7 @@
import asyncio import asyncio
from asyncio import IncompleteReadError from asyncio import IncompleteReadError
from hbmqtt.codecs.errors import NoDataException from hbmqtt.codecs.errors import NoDataException
from math import ceil
def bytes_to_hex_str(data): def bytes_to_hex_str(data):
""" """
@ -21,13 +22,16 @@ def bytes_to_int(data):
""" """
return int.from_bytes(data, byteorder='big') 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 convert an integer to a sequence of bytes using big endian byte ordering
:param int_value: integer value to convert :param int_value: integer value to convert
:return: byte sequence :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 @asyncio.coroutine