retrieve bitwise float representation

Peter Otten __peter__ at web.de
Wed Jun 10 16:26:46 CEST 2009


Ulrich Eckhardt wrote:

> I need to pack a floating point value into a vector of 32-bit unsigned
> values in IEEE format. Further, I maintain a CRC32 checksum for integrity
> checking. For the latter, I actually need the float as integral value.
> 
> What I currently do is this:
> 
>   tmp = struct.pack("=f", f)
>   (i,) = struct.unpack("=L", tmp)
> 
> IOW, I pack and unpack the float using the struct module, which works.
> 
> 
> What I'm wondering is whether there are any better or alternative ways to
> achieve this, the overhead now seems enormous and unnecessary to me here.

binascii.crc32() operates on strings, so you don't need to unpack.
If you have many float values array.array() should be more effective than 
struct.pack().

>>> some_floats = map(float, range(5))
>>> binascii.crc32(array.array("f", some_floats))
1758053516





More information about the Python-list mailing list