n Python 3, if you are sure that :data:`n` can fit into :data:`blocksize` bytes, you can simply use the native method instead:: >>> n.to_bytes(blocksize, 'big') For instance:: >>> n = 80 >>> n.to_bytes(2, 'big') b'\x00P' However, and unlike this ``long_to_bytes()`` function, an ``OverflowError`` exception is raised if :data:`n` does not fit. r