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

K

kuaile xu

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

Thank you very much,
 
C

Chris Rebert

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.(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
 
K

kuaile xu

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

Thanks a lot.
 
I

Ian Kelly

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.

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
 
H

Hrvoje Niksic

Chris Rebert said:
C does not have a built-in fixed-point datatype, so the `struct`
module doesn't handle fixed-point numbers directly.

The built-in decimal module supports fixed-point arithmetic, but the
struct module doesn't know about it. A bug report (or patch) by someone
who works with binary representations of fixed-point would be a good
start to improve it.
 
M

Mark Dickinson

The built-in decimal module supports fixed-point arithmetic, but the
struct module doesn't know about it.  A bug report (or patch) by someone
who works with binary representations of fixed-point would be a good
start to improve it.

Not really: the decimal module is for floating-point, not fixed-
point, though its semantics for significant trailing zeros can help
give the appearance of fixed-point arithmetic for simple operations
(addition, subtraction) that stay within suitable bounds. And decimal
fixed-point isn't so much use for a binary fixed-point format, anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top