hex string to floating point (newbie)

Cameron Laird claird at lairds.com
Mon Mar 17 09:15:34 EST 2003


In article <Fskda.70461$pG1.1629156 at news1.tin.it>,
Alex Martelli  <aleax at aleax.it> wrote:
>zif wrote:
>
>> I would appreciate if somebody could help me solve my problem:
>> I need to translate a hex string '3f8ccccd' to it's
>> floating point value.
>
>Not sure, offhand, if there's a more direct way, one way is
>going through an int, then module struct -- packing the "int"
>into a 4-byte string, then unpacking the latter as a float:
>
>>>> x='3f8ccccd'
>>>> struct.unpack('f',struct.pack('i',int(x,16)))
>(1.1000000238418579,)
>>>>
			.
			.
			.
As Alex knows, but zif might not, this is, let's say,
platform-dependent.  Expect complications if you try
to move to SPARC-, AXP-, ... based operating systems.
-- 

Cameron Laird <Cameron at Lairds.com>
Business:  http://www.Phaseit.net
Personal:  http://phaseit.net/claird/home.html




More information about the Python-list mailing list