Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!txtfeed1.tudelft.nl!tudelft.nl!txtfeed2.tudelft.nl!amsnews11.chello.com!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'distinct': 0.05; 'python': 0.08; '32-bit': 0.09; '>>>>': 0.09; 'manipulation': 0.09; 'parsing': 0.09; 'similarly,': 0.09; 'struct': 0.09; "subject:')": 0.09; 'win32': 0.12; 'converting': 0.15; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; 'fixed-point': 0.16; 'subject:\\': 0.16; 'unpack': 0.16; 'cc:addr:python-list': 0.16; 'wed,': 0.17; 'wrote:': 0.18; '3.2': 0.18; 'bytes': 0.18; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; '(or': 0.22; "doesn't": 0.22; 'feb': 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.212.46': 0.23; 'received:mail- vw0-f46.google.com': 0.23; 'cc:2**0': 0.24; 'module': 0.26; 'expect': 0.26; 'import': 0.27; 'bit': 0.28; 'script': 0.28; 'message-id:@mail.gmail.com': 0.28; 'skip:b 20': 0.29; 'cc:addr:python.org': 0.29; 'nov': 0.29; 'pm,': 0.29; 'chris': 0.30; 'skip:( 20': 0.31; 'does': 0.32; 'error.': 0.32; 'yourself.': 0.34; 'received:209.85.212': 0.34; 'something': 0.35; 'are:': 0.35; 'however,': 0.36; 'two': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'correctly': 0.39; 'third': 0.40; 'received:209': 0.40; 'once': 0.60; 'range': 0.61; 'more': 0.61; 'type': 0.61; '2011': 0.61; 'number.': 0.66; 'it)': 0.67; 'directly.': 0.68; '30,': 0.74; 'introduce': 0.80; 'mp4': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=7E4yRclZCbTR1GLB8LK/A1e4JDZzXzrjXjgjXj3koZg=; b=F/MxMDBHmuNAwb7Fy+Y/0N+QkbkbZYtnYXLqqylfZ+PY3lfatppx6WKbdnAdDGi2BW 6gJnjIDLa7jgo437ZbLHUIhmW4GuhN3KjFYOSaBo1zdGcm9hnZd9Jon7+geIoID727QP zVBzBY9oT3+afK5yKYUUjdIO2FuZ886bnpF+Q= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> References: <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> Date: Wed, 30 Nov 2011 15:02:53 -0800 X-Google-Sender-Auth: HpuOoefnsh4589JmInSrqEZf_r0 Subject: Re: unpack('>f', b'\x00\x01\x00\x00') From: Chris Rebert To: kuaile xu Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322694176 news.xs4all.nl 6842 [2001:888:2000:d::a6]:43586 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16461 On Wed, Nov 30, 2011 at 2:24 PM, kuaile xu 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