To pack an arbitrary long length in Python 2.x, you can use the following:
>>> n = 123456789012345678901234567890L >>> h = '%x' % n >>> s = ('0'*(len(h) % 2) + h).decode('hex') >>> s '\x01\x8e\xe9\x0f\xf6\xc3s\xe0\xeeN?\n\xd2'
This prints the number in big-endian order; for the small end, change the line ( s[::-1] ).
source share