convert floats to their 4 byte representation

G

godavemon

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.
 
A

Alex Martelli

godavemon said:
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
 
T

Tim Chase

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:
.... return "%0*X" % (size, unpack("q", pack("d", f))[0])
....'000040091EB851EB851F'

It's ugly, it's hackish, it's likely architecture-dependant, but
it seems to do what you're describing.

-tkc
 
S

Scott David Daniels

godavemon said:
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())

--Scott David Daniels
(e-mail address removed)
 
G

godavemon

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!

godavemon said:
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())

--Scott David Daniels
(e-mail address removed)
 
C

Cameron Laird

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!

.
.
.
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 >.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top