Extract double in binary file
Bengt Richter
bokr at oz.net
Mon Dec 1 13:31:00 EST 2003
More information about the Python-list mailing list
Mon Dec 1 13:31:00 EST 2003
- Previous message (by thread): Extract double in binary file
- Next message (by thread): Extract double in binary file
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 1 Dec 2003 08:45:04 -0800, pascal.parent at free.fr (Pascal) wrote: >First thanks for trying! > >May be these values will tell you somethings: >1 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x81 >-1 0x0 0x0 0x0 0x0 0x0 0x0 0x80 0x81 >2 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x82 >-2 0x0 0x0 0x0 0x0 0x0 0x0 0x80 0x82 >3 0x0 0x0 0x0 0x0 0x0 0x0 0x40 0x82 >-3 0x0 0x0 0x0 0x0 0x0 0x0 0xc0 0x82 >1.1 0xcd 0xcc 0xcc 0xcc 0xcc 0xcc 0xc 0x81 >1.2 0x9a 0x99 0x99 0x99 0x99 0x99 0x19 0x81 >1.3 0x66 0x66 0x66 0x66 0x66 0x66 0x26 0x81 >1.4 0x33 0x33 0x33 0x33 0x33 0x33 0x33 0x81 >1.01 0x48 0xe1 0x7a 0x14 0xae 0x47 0x1 0x81 >0.01 0xd7 0xa3 0x70 0x3d 0xa 0xd7 0x23 0x7a >1.02 0x8f 0xc2 0xf5 0x28 0x5c 0x8f 0x2 0x81 >0.02 0xd7 0xa3 0x70 0x3d 0xa 0xd7 0x23 0x7b This needs some cleanup and optimization, but for the above it seems to work: ====< PascalParent.py >================================= #First thanks for trying! #May be these values will tell you somethings: data = """\ 1 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x81 -1 0x0 0x0 0x0 0x0 0x0 0x0 0x80 0x81 2 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x82 -2 0x0 0x0 0x0 0x0 0x0 0x0 0x80 0x82 3 0x0 0x0 0x0 0x0 0x0 0x0 0x40 0x82 -3 0x0 0x0 0x0 0x0 0x0 0x0 0xc0 0x82 1.1 0xcd 0xcc 0xcc 0xcc 0xcc 0xcc 0xc 0x81 1.2 0x9a 0x99 0x99 0x99 0x99 0x99 0x19 0x81 1.3 0x66 0x66 0x66 0x66 0x66 0x66 0x26 0x81 1.4 0x33 0x33 0x33 0x33 0x33 0x33 0x33 0x81 1.01 0x48 0xe1 0x7a 0x14 0xae 0x47 0x1 0x81 0.01 0xd7 0xa3 0x70 0x3d 0xa 0xd7 0x23 0x7a 0.0 0x0 0x0 0x0 0x0 0x0 0x0 0x7F 0x0 """ def bytes2float(bytes): if bytes == [0,0,0,0,0,0,0x7f,0]: return 0.0 b = bytes[:] sign = bytes[-2]&0x80 b[-2] |= 0x80 # hidden most significant bit in place of sign exp = bytes[-1] - 0x80 -56 # exponent offset acc = 0L for i,byte in enumerate(b[:-1]): acc |= (long(byte)<<(i*8)) return (float(acc)*2.0**exp)*((1.,-1.)[sign!=0]) for line in data.splitlines(): nlist = line.split() fnum = float(nlist[0]) le_bytes = map(lambda x:int(x,16) ,nlist[1:]) test = bytes2float(le_bytes) print ' in: %r\nout: %r\n'%(fnum,test) ======================================================== Result: [10:44] C:\pywk\clp>PascalParent.py in: 1.0 out: 1.0 in: -1.0 out: -1.0 in: 2.0 out: 2.0 in: -2.0 out: -2.0 in: 3.0 out: 3.0 in: -3.0 out: -3.0 in: 1.1000000000000001 out: 1.1000000000000001 in: 1.2 out: 1.2 in: 1.3 out: 1.3 in: 1.3999999999999999 out: 1.3999999999999999 in: 1.01 out: 1.01 in: 0.01 out: 0.01 in: 0.0 out: 0.0 HTH Regards, Bengt Richter
- Previous message (by thread): Extract double in binary file
- Next message (by thread): Extract double in binary file
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list