convert floats to their 4 byte representation

Cameron Laird claird at lairds.us
Wed Jun 14 12:48:57 EDT 2006


In article <1150301012.403651.198440 at g10g2000cwb.googlegroups.com>,
godavemon <davefowler at gmail.com> wrote:
>I've been a member for a while but I had no idea how helpful this form
>is.  I had a one hour meeting and when I came back there were 4
>replies.  Thanks for your help!
>
>
>Scott David Daniels wrote:
>> godavemon wrote:
>> > I need to take floats and dump out their 4 byte hex representation.
			.
			.
			.
>>      import array
>>
>>      def eights(number, swap=False):
>>          data = array.array('d', [number])
>>          if swap:
>>              data.byteswap()
>>          return ' '.join(hexx(ord(char), 2) for char in data.tostring())
>>
>>      def fours(number, swap=False):
>>          data = array.array('f', [number])
>>          if swap:
>>              data.byteswap()
>>          return ' '.join(hexx(ord(char), 2) for char in data.tostring())
			.
			.
			.
I want to reinforce and refine a bit of what's been written.

comp.lang.python *is* unusually useful.  Note, by the way, that,
among the four replies you first found, ALL FOUR were accurate
and pertinent.  In a world where a discussion group is better-than-
average when *one* out of four of its replies is trustworthy, clp
rates high.

Please be aware that "their ... hex representation" is ambiguous.
At the very least, you need to be sensitive--as perhaps you already
are--to the possibility that a float's (or double's) representation
in memory is hardware-dependent (and, depending on what you mean,
potentially dependent on the version of Python implementation).  If
you pursue this area, you'll want to be on the look-out for "IEEE
754", the standard which ... well, is most standard <URL:
http://en.wikipedia.org/wiki/IEEE_floating-point_standard >.



More information about the Python-list mailing list