generated from Coding-Cuddles/bootstrap-python-kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_converter.py
40 lines (31 loc) · 1.38 KB
/
bit_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def _int_to_bytes_helper(value, length, *, signed=False):
return list(value.to_bytes(length, byteorder="little", signed=signed))
class BitConverter:
@staticmethod
def int16_to_bytes(value):
"""Convert the specified 16-bit signed integer value to bytes."""
return _int_to_bytes_helper(value, 2, signed=True)
@staticmethod
def uint16_to_bytes(value):
"""Convert the specified 16-bit unsigned integer value to bytes."""
return _int_to_bytes_helper(value, 2)
@staticmethod
def int32_to_bytes(value):
"""Convert the specified 32-bit signed integer value to bytes."""
return _int_to_bytes_helper(value, 4, signed=True)
@staticmethod
def uint32_to_bytes(value):
"""Convert the specified 32-bit unsigned integer value to bytes."""
return _int_to_bytes_helper(value, 4)
@staticmethod
def int64_to_bytes(value):
"""Convert the specified 64-bit signed integer value to bytes."""
return _int_to_bytes_helper(value, 8, signed=True)
@staticmethod
def uint64_to_bytes(value):
"""Convert the specified 64-bit unsigned integer value to bytes."""
return _int_to_bytes_helper(value, 8)
@staticmethod
def bytes_to_int64(byte_list):
"""Convert a list of bytes to a 64-bit signed integer."""
return int.from_bytes(bytes(byte_list), "little", signed=True)