Does any one recognize this binary data storage format

Bengt Richter bokr at oz.net
Wed Aug 10 00:29:27 EDT 2005


On Wed, 10 Aug 2005 03:47:06 GMT, bokr at oz.net (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
>
>;-)
>
Now the easy way ;-)

 >>> import struct
 >>> def d2d(h):
 ...     return struct.unpack('d',''.join(chr(int(h[i:i+2],16)) for i in xrange(0,16,2)))[0]
 ...
 >>> d2d('000000806a6e4941')
 3333333.0
 >>> d2d('000000806a6e5941')
 6666666.0
 >>> d2d('0000108777F9Fc41')
 7777777777.0

Regards,
Bengt Richter



More information about the Python-list mailing list