code: def long_to_bytes(n, blocksize=0):
      
      # after much testing, this algorithm was deemed to be the fastest
      s = b''
      n = int(n)
      pack = struct.pack
      while n > 0:
          s = pack('>I', n & 0xffffffff) + s
          n = n >> 32
      # strip off leading zeros
      for i in range(len(s)):
          if s[i] != b'\000'[0]:
              break
      else:
          # only happens when n == 0
          s = b'\000'
          i = 0
      s = s[i:]
      # add back some pad bytes.  this could be done more efficiently w.r.t. the
      # de-padding being done above, but sigh...
      if blocksize > 0 and len(s) % blocksize:
          s = (blocksize - len(s) % blocksize) * b'\000' + s
      return s
docs: Convert an integer to a byte string.
  
      In Python 3.2+, use the native method instead::
  
          >>> n.to_bytes(blocksize, 'big')
  
      For instance::
  
          >>> n = 80
          >>> n.to_bytes(2, 'big')
          b'\x00P'
  
      If the optional :data:`blocksize` is provided and greater than zero,
      the byte string is padded with binary zeros (on the front) so that
      the total length of the output is a multiple of blocksize.
  
      If :data:`blocksize` is zero or not provided, the byte string will
      be of minimal length.