Does any one recognize this binary data storage format
John Machin
sjmachin at lexicon.net
Wed Aug 10 00:28:05 EDT 2005
Bengt Richter wrote:
> On Tue, 09 Aug 2005 21:50:06 -0000, Grant Edwards <grante at visi.com> wrote:
>
>
>>On 2005-08-09, Scott David Daniels <Scott.Daniels at Acm.Org> wrote:
>>
>>>Grant Edwards wrote:
>>>
>>>>>Ex #1) 333-3333
>>>>>Hex On disk: 00 00 00 80 6a 6e 49 41
>>>>>
>>>>>Ex #2) 666-6666
>>>>>Hex On disk: 00 00 00 80 6a 6e 59 41
>>>>
>>>>So there's only a 1-bit different between the on-disk
>>>>representation of 333-3333 and 666-6666.
>>>>
>>>>That sounds pretty unlikely. Are you 100% sure you're looking
>>>>at the correct bytes?
>>>
>>>Perhaps the one bit is an exponent -- some kind of floating point
>>>based format? That matches the doubling of all digits.
>>
>>That would just be sick. I can't imagine anybody on an 8-bit
>>CPU using FP for a phone number.
>>
>>--
>>Grant
>>
>
> >>> def double_binary_lehex_to_double(dhex):
> ... "convert little-endian hex of ieee double binary to double"
> ... assert len(dhex)==16, (
> ... "hex of double in binary must be 8 bytes (hex pairs in little-endian order")
> ... dhex = ''.join(reversed([dhex[i:i+2] for i in xrange(0,16,2)]))
> ... m = int(dhex, 16)
> ... x = ((m>>52)&0x7ff) - 0x3ff - 52
> ... s = (m>>63)&0x1
> ... f = (m & ((1<<52)-1))|((m and 1 or 0)<<52)
> ... return (1.0,-1.0)[s]*f*2.0**x
> ...
> >>> double_binary_lehex_to_double('000000806a6e4941')
> 3333333.0
> >>> double_binary_lehex_to_double('000000806a6e5941')
> 6666666.0
> >>> double_binary_lehex_to_double('0000108777F9Fc41')
> 7777777777.0
>
> ;-)
>
> Regards,
> Bengt Richter
Well done, Scott & Bengt!!
I've just verified that this works with all 12 corrected examples posted
by the OP.
Grant, MS-DOS implies 16 bits at least; and yes there was an FPU (the
8087). And yes there are a lot of sick people who store things as
numbers (whether integer or FP) when the only arithmetic operations that
can be applied to them stuff them up mightily (like losing leading
zeroes off post-codes, having NEGATIVE tax file numbers, etc) and it's
still happening on the best OSes and 64-bit CPUS. Welcome to the real
world :-)
Cheers,
John
More information about the Python-list
mailing list