Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16465

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'bits': 0.07; 'python': 0.08; '16-bit': 0.09; '32-bit': 0.09; '>>>>': 0.09; 'parsing': 0.09; 'struct': 0.09; "subject:')": 0.09; 'win32': 0.12; 'float': 0.13; 'converting': 0.15; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '-16': 0.16; 'fixed-point': 0.16; 'overflow': 0.16; 'precision,': 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; 'int': 0.18; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'feb': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'import': 0.27; 'bit': 0.28; 'script': 0.28; 'message-id:@mail.gmail.com': 0.28; 'assuming': 0.29; 'fixed': 0.29; 'skip:b 20': 0.29; 'cc:addr:python.org': 0.29; 'nov': 0.29; 'pm,': 0.29; 'float.': 0.30; 'precision': 0.30; 'received:mail- bw0-f46.google.com': 0.30; 'chris': 0.30; 'skip:( 20': 0.31; 'value.': 0.32; 'received:209.85.214': 0.32; "can't": 0.32; 'there': 0.33; 'match': 0.34; 'scale.': 0.34; 'are:': 0.35; 'however,': 0.36; 'addition,': 0.36; 'two': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'could': 0.37; 'received:209.85': 0.38; 'should': 0.39; 'third': 0.40; 'received:209': 0.40; 'once': 0.60; 'more': 0.61; 'type': 0.61; '2011': 0.61; 'direction.': 0.64; 'number.': 0.66; '30,': 0.74; 'mp4': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=/gaoZHT0bpLbHPRtujc1Vboz+4WnVc6+u3EOP3HwpeE=; b=fiOEo5yy18T+HtzCbmRfXs1bl99z9cUgyYCuU49lTpRpjyKTTv7mF+xDB5xh7k1rr5 aaEVzzFRq27TfCRZ+qSGi0jk/JpoakT5LyGxxDpxV0RMo0nhI5g7ewjlrssQnhEKxfiD aO63KKohWdPX3xRrjGry/Hw41TBwEBHa0xKz4=
MIME-Version 1.0
In-Reply-To <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com>
References <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 30 Nov 2011 16:49:52 -0700
Subject Re: unpack('>f', b'\x00\x01\x00\x00')
To kuaile xu <kuaile.xu@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3184.1322697025.27778.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1322697025 news.xs4all.nl 6925 [2001:888:2000:d::a6]:50141
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16465

Show key headers only | View raw


On Wed, Nov 30, 2011 at 3:24 PM, kuaile xu <kuaile.xu@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,)
>>>>

Like Chris said, you can't unpack it as a float.  Assuming that's Q16
fixed-point, what you can do is unpack it as an int and then multiply
by 2.0 ** -16 to get the corresponding float value.  There should be
no loss of precision converting to a float since floats store 52 bits
of precision, but be aware that you could have overflow or loss of
precision when converting in the opposite direction.

> In addition, there is another field which is a 16-bit fixed point
> number. How do I unpack two bytes into a float?

Same as above, just change the multiplier to match the scale.

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

unpack('>f', b'\x00\x01\x00\x00') kuaile xu <kuaile.xu@gmail.com> - 2011-11-30 14:24 -0800
  Re: unpack('>f', b'\x00\x01\x00\x00') Chris Rebert <clp2@rebertia.com> - 2011-11-30 15:02 -0800
    Re: unpack('>f', b'\x00\x01\x00\x00') kuaile xu <kuaile.xu@gmail.com> - 2011-11-30 15:25 -0800
    Re: unpack('>f', b'\x00\x01\x00\x00') Hrvoje Niksic <hniksic@xemacs.org> - 2011-12-01 11:21 +0100
      Re: unpack('>f', b'\x00\x01\x00\x00') Mark Dickinson <mdickinson@enthought.com> - 2011-12-02 08:25 -0800
  Re: unpack('>f', b'\x00\x01\x00\x00') Ian Kelly <ian.g.kelly@gmail.com> - 2011-11-30 16:49 -0700

csiph-web