Newbie: How To Convert A Float Into Two Sixteen Bit Integers.....?
Skip Montanaro
skip at pobox.com
Thu Sep 13 16:46:26 EDT 2001
John> Can any tell me how I can take a single precision floating point
John> number and convert it into two sixteen bit integers using Python?
John> I need to store some floating point numbers in a eight bit wide
John> EEPROM.
Take a look at the struct module.
http://www.python.org/doc/current/lib/module-struct.html
I suspect you want to pack the float and unpack two shorts:
>>> import struct
>>> f = 6.02e23
>>>
>>> st = struct.pack("f", f)
>>> s1, s2 = struct.unpack("HH", st)
>>> s1, s2
(62713, 26366)
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list