pack an integer into a string

superpollo user at example.net
Fri Jul 24 19:16:07 EDT 2009


Piet van Oostrum wrote:
...
> You have the string wrong.

oops yea.

> But the correct one you get with:
> 
> In [67]: import struct
> 
> In [68]: number = 252509952
> 
> In [69]: struct.pack('>I', number)
> Out[69]: '\x0f\x0c\xff\x00'
>
> (Please note that this is big endian)

thanks a lot, but it does not work for large integers:

  >>> number
  283691163101781L
  >>> struct.pack('>I', number)
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  OverflowError: long int too large to convert
  >>>

bye



More information about the Python-list mailing list