Re: pack an integer into a string

2009-07-24 Thread Paul Rubin
superpollo writes: > >>> number = 252509952 > >>> hex(number) > '0xf0cff00' > >>> > > so i would like a string like '\xf0\xcf\xf0\x00' def encode(number): h = '%x' % number if len(h) % 2 == 1: h = '0' + h return h.decode('hex') -- http://mail.python.org/mailman/listinfo/p

Re: pack an integer into a string

2009-07-24 Thread casevh
On Jul 24, 3:28 pm, superpollo 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: > >   >>> number = 252509952 >   >>> hex(number) >   '0xf0cff00' >   >>> > > so i would like a string like '\xf0\xcf\

Re: pack an integer into a string

2009-07-24 Thread John Yeung
On Jul 24, 7:16 pm, superpollo wrote: > thanks a lot, but [struct] does not work for large integers: Since the struct module is designed specifically for C-style structs, it's definitely not going to handle arbitrary-length integers on its own. You could chop up your Python (long) integer into C

Re: pack an integer into a string

2009-07-24 Thread superpollo
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 f

Re: pack an integer into a string

2009-07-24 Thread superpollo
superpollo wrote: Sebastian Bassi wrote: On Fri, Jul 24, 2009 at 7:28 PM, superpollo 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.

Re: pack an integer into a string

2009-07-24 Thread Piet van Oostrum
> superpollo (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'

Re: pack an integer into a string

2009-07-24 Thread superpollo
Sebastian Bassi wrote: On Fri, Jul 24, 2009 at 7:28 PM, superpollo 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 variab

Re: pack an integer into a string

2009-07-24 Thread Sebastian Bassi
On Fri, Jul 24, 2009 at 7:28 PM, superpollo 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 c

pack an integer into a string

2009-07-24 Thread superpollo
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: >>> number = 252509952 >>> hex(number) '0xf0cff00' >>> so i would like a string like '\xf0\xcf\xf0\x00' i wrote some code to do it, so ugly i am ashame