1234567891011121314151617181920212223 |
- from typing import Optional
- def bytes_to_long(buffer: bytes, buffer_size: Optional[int] = None) -> int:
- if buffer_size is not None:
- assert len(buffer) == buffer_size
- num = 0
- for byte in buffer:
- num = num * 256 + byte
- return num
- def long_to_bytes(num: int, buffer_size: int) -> bytes:
- assert 0 <= num
- buffer = [0] * buffer_size
- for i in range(buffer_size):
- buffer[-i - 1] = num & 255
- num >>= 8
- return bytes(buffer)
|