Newsgroups: comp.lang.python
From: "Francis Avila" <francisgav...@yahoo.com>
Date: Fri, 28 Nov 2003 04:45:31 -0500
Local: Fri, Nov 28 2003 4:45 am
Subject: Re: Extract double in binary file
Dennis Lee Bieber wrote in message ... That is a bizarre format, and of course I had to implement it. (Even C is >Pascal fed this fish to the penguins on Thursday 27 November 2003 01:07 >am: >> Some precisions: >> 10.00 > 00-00-00-00-00-00-20-84 >> 1.11 > 14-AE-47-E1-7A-14-0E-81 > 0 1 00 00 00 00 00 00 00 more pleasant in Python!). It works for the cases given, but do find out where the sign bit is for the Also tease out the NaN and +-Infinity cases. --- Code --- import struct _known = (('0.00', '\x00\x00\x00\x00\x00\x00\x7F\x00'), def _test(): failed = [tests[i] for i, passed in enumerate(results) if not passed] if failed: return failed def bin(I): def binaryE(n, exp): As n*10**exp is to decimal, so binaryE(n, exp) is to binary. #Add special cases here: def dectofloat(S): # Convert to byteswapped long. # Grab exponent and mantissa parts using bitmasks. exp -= 0x81 # Exponential part is excess 0x81 (e.g., 0x82 is 1). msign = mant & (0x80L<<48) # MSB of mantissa is sign bit. 0==positive. mant |= (0x80L<<48) # Add implied 1 to the MSB of mantissa. # Now, binary scientific notation: return float(msign * binaryE(mant, exp)) You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||