Questions tagged [unpack]

A function in several scripting languages for unpacking binary data into native types for the language in question. The opposite of the 'pack' function. For the Python concept, use 'iterable-unpacking'. This tag should be used with a programming language tag as well. While also used as directive or pragma in some compilers to ignore standard variable alignment for data aggregates (i.e. struct), do not use this tag for posts on that subject.

The unpack function parses a binary string into several variables with types that are native to the language in question. The binary string usually matches the machine-level representation that you would see in a C . The inverse operation is .

For Python use .

Most versions of unpack and pack functions use a format or template that specifies the byte by byte layout of the data being copied out of or put into the memory area containing the binary representation.

Both functions allow the use of C or C++ routines with complex data structures to be used with the scripting language in order to improve application performance or to perform operations and actions not available in the scripting language.

Documentation:

615 questions
6
votes
5 answers

Decoding packed data into a structure

What would the best way of unpacking a python string into fields I have data received from a tcp socket, it is packed as follows, I believe it will be in a string from the socket recv function It has the following format uint8 - header uint8 -…
mikip
  • 1,677
  • 6
  • 25
  • 35
6
votes
2 answers

Unpacking nested C structs in Python

I am trying to unpack a C struct that is handed to my Python program in binary form and includes another nested struct. The relevant part of the C header looks like this: typedef struct { uint8_t seq; uint8_t type; uint16_t flags; …
jpfender
  • 144
  • 2
  • 12
6
votes
1 answer

Unexpected unpack results with bit strings

Why is it that when I open up irb and I run puts 'A'.unpack("B8") I get 01000001 but when I run puts 'A'.unpack("B4B4") I only get 0100 and not [0100,0001]? Is the resolution of unpack only a full byte? Nothing less?
Justin
  • 1,428
  • 1
  • 13
  • 29
6
votes
3 answers

Ruby String#unpack

I have a packed string of 3 strings that is composed in a way so that I have an integer, specifying the byte length of the next item and then that item's bytes and then the next item's bytesize, etc. as if somebody did: [a.bytesize, a, b.bytesize,…
Speed
  • 1,424
  • 1
  • 16
  • 24
6
votes
4 answers

What does the comma mean in Python's unpack?

We can simply use: crc = struct.unpack('>i', data) why do people write it like this: (crc,) = struct.unpack('>i', data) What does the comma mean?
Codefor
  • 1,318
  • 2
  • 13
  • 22
6
votes
1 answer

Unexpected behavior of PHP unpack()

Tests $x = sprintf( "foo\x00bar\x00baz" ); $y = unpack( 'afoo/abar/abaz' , $x ); print_r( $y ); $x = sprintf( "foo\x00bar\x00baz" ); $y = unpack( 'a*foo/a*bar/a*baz' , $x ); print_r( $y ); Results Array ( [foo] => f [bar] => o [baz]…
cYrus
  • 2,976
  • 6
  • 29
  • 47
6
votes
2 answers

Why is Haskell/unpack messing with my bytes?

I've built a tiny UDP/protobuf transmitter and receiver. I've spent the morning trying to track down why the protobuf decoding was producing errors, only to find that it was the transmitter (Spoke.hs) which was sending incorrect data. The code…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
5
votes
2 answers

Unpack format characters in Python

I need the Python analog for this Perl string: unpack("nNccH*", string_val) I need the nNccH* - data format in Python format characters. In Perl it unpack binary data to five variables: 16 bit value in "network" (big-endian) 32 bit value in…
Sir D
  • 2,164
  • 2
  • 17
  • 21
5
votes
3 answers

Using bundler, how do you unpack a gem installed via a git repository?

How can I unpack a gem specified in bundler by a :git => url? My Gemfile has gem 'my_gem', :git => 'git@github.com:xxxxx/xxxxx.git' $ bundle correctly reports the gem as available, and my code works. $ bundle which my_gem even tells me where my gem…
Gareth
  • 133,157
  • 36
  • 148
  • 157
5
votes
1 answer

Is it possible to skip over bytes when using Python's struct.unpack?

It is possible to use Python's struct.unpack to extract the useful values from a Bitmap file header, as follows: magic, file_size, _, _, data_offset = struct.unpack('<2sLHHL', file_header) assert magic == 'BM' Is there any way to avoid the need to…
user200783
  • 13,722
  • 12
  • 69
  • 135
5
votes
1 answer

Git Push to Network Drive: Cannot pread pack file

I'm trying to push locally to a shared repo on a network drive. I'm getting the following error: :~/git push origin master Counting objects ... done Writing objects ... Total .... but then: fatal: cannot pread pack file: No Permission error: unpack…
ndbd
  • 2,417
  • 3
  • 23
  • 32
5
votes
1 answer

How to download and unpack .ZIP folder using Adobe Air?

How to download and unpack .ZIP folder using Adobe Air? So I have http link onto that zip file example.com/zip.zip I need a function to download it onto users hard drive and unpack it into some folder on filesystem. How to do such thing? (code…
Rella
  • 65,003
  • 109
  • 363
  • 636
5
votes
3 answers

python ValueError: too many values to unpack in tuple

So I am extracting data from a JSON file. I am trying to pack my data in a way so a preprocessing script can use it. preprocessing script code: for key in split: hist = split[key] for text, ans, qid in hist: Now I have an extracted dataset…
KameeCoding
  • 693
  • 2
  • 9
  • 27
5
votes
1 answer

PHP convert byte string into integer

I am trying to convert a 2 byte string into a Short/int data type with unpack but it does not seem to work: $str = "\x01\xBB"; unpack("S",$str); it gives 47873 where as it must return 443
asim-ishaq
  • 2,190
  • 5
  • 32
  • 55
5
votes
5 answers

Python: How to transfer varrying length arrays over a network connection

I need to transfer an array of varying length in which each element is a tuple of two integers. As an example: path = [(1,1),(1,2)] path = [(1,1),(1,2),(2,2)] I am trying to use pack and unpack, however, since the array is of varying length I…
Devin
  • 81
  • 2
  • 9