pack an integer into a string

John Yeung gallium.arsenide at gmail.com
Fri Jul 24 20:14:59 EDT 2009


On Jul 24, 7:16 pm, superpollo <u... at example.net> 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-sized pieces
and then pack the pieces; but if you need to do that, the advantage of
using struct at all may be minimal or even completely gone.

> OTOH, using a higher [pickle] protocol:

If you are sure you are going to keep using the same version of
Python, then this could work.  I would personally not depend on this
to keep working in future versions.

I'm not aware of any existing package that does what you want.  Maybe
someone else does.  How I personally would tackle your problem is to
write the whole thing from scratch (as you've done).

John



More information about the Python-list mailing list