retrieve bitwise float representation
Tom K.
tpk at kraussfamily.org
Thu Jun 11 09:20:27 EDT 2009
On Jun 10, 8:41 am, Ulrich Eckhardt <eckha... at satorlaser.com> 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'm wondering is whether there are any better or alternative ways to
> achieve this, the overhead now seems enormous and unnecessary to me here.
Numpy has support for this:
import numpy as np
a = np.arange(10.) # an array of floats
b = a.view(dtype=np.uint32)
print b
array([ 0, 0, 1072693248, 0, 1073741824,
0, 1074266112, 0, 1074790400, 0,
1075052544, 0, 1075314688, 0, 1075576832,
0, 1075838976, 0, 1075970048, 0],
dtype=uint32)
b[0]=5
print a
array([ 1.06099790e-313, 1.00000000e+000, 2.00000000e+000,
3.00000000e+000, 4.00000000e+000, 5.00000000e+000,
6.00000000e+000, 7.00000000e+000, 8.00000000e+000,
9.00000000e+000])
More information about the Python-list
mailing list