Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack binary data with python

I would like to unpack an array of binary data to uint16 data with Python.

Internet is full of examples using struct.unpack but only examples dealing with binary array of size 4.

Most of these examples are as follow (B is a binary array from a file)

U = struct.unpack("HH",B[0:4]);

So i tried to unpack an array of size 6:

U = struct.unpack("HHH",B[0:6]);

It works.

But how to do if I want to unpack an array of size L (L is pair)? I tried that:

U = struct.unpack("H"*(L/2),B[0:L]);

but it doesn't work, prompter gives me an error (for L=512 for example):

struct.error: unpack requires a string argument of length 512

This message is strange because if i want to unpack a binary array to uint16, I need a string "HHH...HHH" of half size of this array...

I would be very grateful if someone could provide me with some help.


I progress a little bit... In fact, i tried:

U = struct.unpack("H"*8,B[0:8]); 

It works.

U = struct.unpack("H"*10,B[0:10]);

It works.

U = struct.unpack("H"*222,B[0:444]);

It still works

U = struct.unpack("H"*223,B[0:446]);

It doesn't work! and it never works for size bigger than 446

Hope it will help anyone to answer me.


@MarkRansom I checked len(B) and in fact, the length is 444. I was so sure that B is an array of size 512 because B comes from : B = f.read(512) where F is a 8000-bytes-size file. So a problem with read()... Thanks for this answer! But if someone has a help to unpack binary array of size L, i would be grateful

like image 518
Alan Turing Avatar asked Nov 15 '12 16:11

Alan Turing


People also ask

What is struct unpack in Python?

unpack() This function unpacks the packed value into its original representation with the specified format. This function always returns a tuple, even if there is only one element.

Is there a binary data type in Python?

In Python, the boolean data type is the binary variable and defined as T r u e or F a l s e . Additionally, the bool() function converts the value of an object to a boolean value. This function returns T r u e for all values except the following values: Empty objects (list, tuple, string, dictionary)

What does struct do in Python?

The struct module in Python is used to convert native Python data types such as strings and numbers into a string of bytes and vice versa. What this means is that users can parse binary files of data stored in C structs in Python.

What does struct Calcsize do?

struct. calcsize('P') calculates the number of bytes required to store a single pointer -- returning 4 on a 32-bit system and 8 on a 64-bit system.


2 Answers

Use array.fromstring or array.fromfile (see http://docs.python.org/2/library/array.html ):

import array
U = array.array("H")
U.fromstring(B)
like image 177
Mikhail Korobov Avatar answered Sep 23 '22 01:09

Mikhail Korobov


Variable length version of the same thing:

n = 999
U = struct.unpack(str(n)+"H", B)
like image 40
trudnai Avatar answered Sep 20 '22 01:09

trudnai