unpack('>f', b'\x00\x01\x00\x00')

Chris Rebert clp2 at rebertia.com
Wed Nov 30 18:02:53 EST 2011


On Wed, Nov 30, 2011 at 2:24 PM, kuaile xu <kuaile.xu at gmail.com> wrote:
> Hi:
>
> I am working on a python script that parses mp4 video header. Once of
> the field is a 32-bit fixed-point number.
>
> I know that the four bytes are: 00, 01, 00, 00. I have a third party
> mp4 parsing program which displays this field's value is:1.0.
>
> However, the struct.unpack gets a value of 0.0.
>
> Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from struct import *
>>>> unpack('>f', b'\x00\x01\x00\x00')
> (9.183549615799121e-41,)

Floating-point and fixed-point are *separate* number formats with
distinct representations. You cannot expect to correctly (un)pack one
as if it was the other. Similarly, converting between the two formats
can introduce range and/or imprecision error.

C does not have a built-in fixed-point datatype, so the `struct`
module doesn't handle fixed-point numbers directly. You're going to
have to unpack it (or parts of it) as something more raw, and then do
the necessary bit manipulation yourself.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list