convert floats to their 4 byte representation
6 answers - 370 bytes -

I need to take floats and dump out their 4 byte hex representation.
This is easy with ints with the built in hex function or even better
for my purpose
def hex( number, size ):
s = "%"+str(size) + "X"
return (s % number).replace(' ', '0')
but I haven't been able to find anything for floats. Any help would be
great.
No.1 | | 1138 bytes |
| 
godavemon <davefowler (AT) gmail (DOT) comwrote:
I need to take floats and dump out their 4 byte hex representation.
This is easy with ints with the built in hex function or even better
for my purpose
def hex( number, size ):
s = "%"+str(size) + "X"
return (s % number).replace(' ', '0')
but I haven't been able to find anything for floats. Any help would be
great.
floats' internal representation in Python is 8 bytes (equivalent to a C
"double"). However, you can import struct and use struct.pack('f',x) to
get a 4-byte ("single precision") approximation of x's 8-byte value,
returned as a string of 4 bytes; you may force little-endian by using as
the format '<f' or big-endian by using '>f'; then loop on each character
of the string and get its ord(c) [a number from 0 to 255] to format as
you wish.
For example
import struct
def float_hex4(f):
return ''.join(('%2.2x'%ord(c)) for c in struct.pack('f', f))
or variants thereof.
Alex
No.2 | | 930 bytes |
| 
s = "%"+str(size) + "X"
return (s % number).replace(' ', '0')
While I don't have a fast and easy way to represent floats, you
may want to tweak this to be
return ("%0*X" % (size,number))
which will zero-pad the number in hex to "size" number of places
in a single step. It also helps prevent problems where there might
but I haven't been able to find anything for floats. Any help
would be great.
My first stab at such an attempt:
from struct import pack, unpack
s = pack("d", 3.14)
i = unpack("q", s)
"%X"%i
'40091EB851EB851F'
def floatAsHex(f, size):
return "%0*X" % (size, unpack("q", pack("d", f))[0])
floatAsHex(3.14, 20)
'000040091EB851EB851F'
It's ugly, it's hackish, it's likely architecture-dependant, but
it seems to do what you're describing.
-tkc
No.3 | | 153 bytes |
| 
2006-06-14, godavemon <davefowler (AT) gmail (DOT) comwrote:
I need to take floats and dump out their 4 byte hex representation.
struct
No.4 | | 1138 bytes |
| 
godavemon wrote:
I need to take floats and dump out their 4 byte hex representation.
This is easy with ints with the built in hex function or even better
for my purpose
def hex( number, size ):
s = "%"+str(size) + "X"
return (s % number).replace(' ', '0')
This should be a trifle nicer:
def hexx(number, size): # don't shadow the "hex" builtin
s = "%0s" + str(size) + "X"
return s % number
but I haven't been able to find anything for floats. Any help would be
great.
Getting to float bytes is tougher.
First, python Floats are C Doubles, so you probably mean 8=byte hex.
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())
David Daniels
scott.daniels (AT) acm (DOT) org
No.5 | | 1336 bytes |
| 
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.
This is easy with ints with the built in hex function or even better
for my purpose
def hex( number, size ):
s = "%"+str(size) + "X"
return (s % number).replace(' ', '0')
This should be a trifle nicer:
def hexx(number, size): # don't shadow the "hex" builtin
s = "%0s" + str(size) + "X"
return s % number
but I haven't been able to find anything for floats. Any help would be
great.
Getting to float bytes is tougher.
First, python Floats are C Doubles, so you probably mean 8=byte hex.
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())
David Daniels
scott.daniels (AT) acm (DOT) org
No.6 | | 1959 bytes |
| 
In article <1150301012.403651.198440 (AT) g10g2000cwb (DOT) googlegroups.com>,
godavemon <davefowler (AT) gmail (DOT) comwrote:
>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 FUR 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 perhaps you already
are 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:
>.