HasteBin0

Python Big Integer to Bytes()

Jul 25th, 2020 (edited)
2,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.29 KB | None | 0 0
  1. from binascii import unhexlify
  2.  
  3. def long_to_bytes(value: int, big_endian: bool) -> bytes:
  4.     # https://stackoverflow.com/a/14527004/3810747
  5.     s = unhexlify(('%%0%dx' % (((width := value.bit_length()) + 8 - ((width % 8) or 8)) // 4)) % value)
  6.     return s if big_endian else s[::-1]
  7.  
  8.  
Add Comment
Please, Sign In to add comment