pack an integer into a string
superpollo
user at example.net
Fri Jul 24 19:11:08 EDT 2009
superpollo wrote:
> Sebastian Bassi wrote:
>
>> On Fri, Jul 24, 2009 at 7:28 PM, superpollo<user at example.net> wrote:
>>
>>> is there a pythonic and synthetic way (maybe some standard module) to
>>> "pack"
>>> an integer (maybe a *VERY* big one) into a string? like this:
>>
>>
>>
>> What do you mean to pack? Maybe Pickle is what you want.
>>
>> import cPickle
>> variable = 124348654333577698
>> cPickle.dump(variable,open('filename', 'w'))
>>
>> To load it:
>>
>> import cPickle
>> vaariable = cPickle.load(open('filename'))
>>
>> It is not "into a string", but instead of a filename, use a string
>> with stringio
>
>
>
> >>> number
> 252509952
> >>> f = cStringIO.StringIO()
> >>> cPickle.dump(number , f)
> >>> f.getvalue()
> 'I252509952\n.'
> >>>
>
> as you can see, the pickled representation is not what i wanted...
>
OTOH, using a higher protocol:
>>> cPickle.dump(number , f , 1)
>>> f.getvalue()
'J\x00\xff\x0c\x0f.'
which is very close to '\xf0\xcf\xf0\x00', that i meant originally... so
i i change my spec as; '\x0f\x0c\xff\x00' (i.e. left- intead of right-
zeropadding) i am almost there...
bye
More information about the Python-list
mailing list