Binary representation of floating point numbers

Grant Edwards grante at visi.com
Tue Dec 6 15:41:17 EST 2005


On 2005-12-06, 63q2o4i02 at sneakemail.com <63q2o4i02 at sneakemail.com> wrote:

> I'm using python to run some lab equipment using PyVisa.  When I read a
> list of values from the equipment, one of the fields is 32 bits of
> flags, but the value is returned as a floating point number, either in
> ASCII format, or pure binary.

Wha?  That doesn't make any sense.  If it's 32 bits of flags,
then it's not a floating point number "in pure binary".  

I suppose you could pretend the 32 bit chunk is a float and
print the resulting value, but that's just insane.  And it
won't work. For some bit patterns, there's no way to go from
ASCII back to that same bit pattern.  IOW, there is not a 1:1
mapping between bit patterns and the string representation, so
if that's what you've got, you're screwed.

> In either case, since I'm using PyVisa, it converts the number
> to a single precision floating point, and that's what I have
> to work with.

It sounds like PyVisa is broken if it's treating someting as a
float when it isn't.

> The question is how do I recover the bits out of this floating
> point value so I can read the flags represented here?

Use the struct module.

> Also, it's little (or big?) endian.  Whatever... how do I
> manipulate the endianness?

Again, the struct module.

-- 
Grant Edwards                   grante             Yow!  ... I have read the
                                  at               INSTRUCTIONS...
                               visi.com            



More information about the Python-list mailing list