IBM integer and double formats

John Machin sjmachin at lexicon.net
Tue Nov 11 00:49:31 CET 2008


On Nov 11, 9:00 am, Mark Dickinson <dicki... at gmail.com> wrote:
> On Nov 10, 7:20 pm, "john.goodleaf" <john.goodl... at gmail.com> wrote:
>
> > my own routines, does anyone know of an already-done means of writing
> > integers and floats out to their IBM mainframe equivalents?
>
> Here's a quick attempt at converting doubles using Python.
> It uses the isnan and isinf functions that are in Python 2.6;
> if you don't have Python 2.6 or don't care about IEEE specials
> then just delete the relevant lines.
>
> Mark
>
> def IBMtoIEEE(x):
>     """Convert a Python float (assumed stored as an IEEE 754 double)
>     to IBM hexadecimal float format.

Call me crazy if you like, but I'd name that function IEEEtoIBM.

> return "%x" % (s*2**63 + e*2**56 + m)

That's a hexadecimal representation in lowercase with no leading
zeroes ... variable length and lowercase doesn't seem very IBM to me.

The extremely ugly C code in
http://support.sas.com/techsup/technote/ts140.html
seems to indicate an 8-byte bigendian binary representation. Note that
page contains a tohex() function which is not used AFAICT.

Perhaps this would do the job:
return struct.pack('>Q', s*2**63 + e*2**56 + m)

Cheers,
John



More information about the Python-list mailing list