Checking if an int fits in 32 bits?

Roy Smith roy at panix.com
Thu Dec 4 15:44:57 EST 2008


On Dec 4, 3:33 pm, Thomas Heller <thel... at python.net> wrote:

> Roy Smith schrieb:
>
> > I'm working with marshaling data over a binary wire protocol.  I'm
> > using struct.pack() to handle the low-level encoding of ints.  One of
> > the things I need to do is make sure an int can be represented in 4
> > bytes.

> You could try something like this:
>
> import ctypes
> def int_fits_in_32bit(value):  
>   return ctypes.c_int32(value) == value

Good idea!  It turns out you've got to do types.c_int32(value).value()
== value, but that does indeed seem to do exactly what I want.
Thanks.




More information about the Python-list mailing list