Reading a binary file

Peter Hansen peter at engcorp.com
Thu Jun 26 11:28:52 EDT 2003


Sorin Marti wrote:
> 
> Byte Number/Length   Type       Hex-Value
> ----------------------------------------------------------------
> Byte 12,13,14,15,16: Real       BB 42 C8 00 00
> 
> you can call
> getValue('REAL',12,16) and you should get back 100 because if you have
> the binary value of 'BB 42 C8 00 00' is 01000010110010000000000000000000
> , first digit is the Sign (which is + or - ), next 8 digits are the
> exponent, in this case 10000101 = 133dec. Now you take away 127 from 133
> then you get six, thats the exponent. The rest
> (110010000000000000000000) has a hex value of C80000 this is 13107200
> decimal. Now you have to multiply 13107200 with 2^6 and 2^-23 and you
> get (tataaaaaa!): 100!

I think you might be interpreting (or explaining?) the format of that
real incorrectly.

If the first bit is the sign, and the next 8 bits are the 
exponent, and the rest is mantissa, then your exponent should
be 01110110 (or h76 or d118) and your mantissa value in hex
would be all of the 42 C8 00 00, or 1120403456 in decimal.

(Basically, your binary value as shown is wrong.  BB42C80000 is really
1010 1010 0100 0010 1100 1000 0000 0000 0000 0000 0000 and not your
value of  0100 0010 1100 1000 0000 0000 0000 0000 as shown above.)

-Peter




More information about the Python-list mailing list