pack an integer into a string

Piet van Oostrum piet at cs.uu.nl
Fri Jul 24 19:03:25 EDT 2009


>>>>> superpollo <user at example.net> (s) wrote:

>s> is there a pythonic and synthetic way (maybe some standard module) to
>s> "pack" an integer (maybe a *VERY* big one) into a string? like this:

>>>>> number = 252509952
>>>>> hex(number)
>s>  '0xf0cff00'
>>>>> 

>s> so i would like a string like '\xf0\xcf\xf0\x00'

You have the string wrong.
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)
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list