Binary representation of floating point numbers
mwm at mired.org
Tue Dec 6 22:02:34 CET 2005
63q2o4i02 at sneakemail.com writes:
> 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.
Value returned by *what*? Your equipment doesn't return floating
point numbers, it returns bytes. Those can be interpreted in any
number of ways. What you have to do is get those bytes into something
that lets you read the bits out of it. How you do that depends on how
you're getting the bytes.
> 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.
If the conversion is anything but trivial, there's a fair chance the
information you're interested is destroyed by it.
> The question is how do I recover the bits out of this floating point
> value so I can read the flags represented here?
I'm not sure this is doable in Python. I'm not sure I *want* it to be
doable in Python. In C, you use a cast.
> Also, it's little (or big?) endian. Whatever... how do I manipulate
> the endianness?
On most processors, you don't. If you can, doing so will cause the OS
to come to a screeching halt on most of them. I don't know if anyone
still building boxes that treats the underlying platform as context to
the degree you're asking for here.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list