Questions tagged [pack]

A function in several scripting languages for packing data, creating a sequence of specifically ordered and aligned bytes, into a binary structure. The opposite of the `unpack` function. This tag should be used with a programming language tag as well. While also used as a directive or pragma in some compilers to cause the compiler to ignore standard variable alignment for data aggregates (i.e. struct), do not use this tag for posts on that subject.

The pack function combines several high-level variables into a single low-level binary representation that usually matches the machine-level representation that you would see in a C struct. The inverse operation is .

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

Documentation:

682 questions
13
votes
2 answers

Why does unpacking a struct result in a tuple?

After packing an integer in a Python struct, the unpacking results in a tuple even if it contains only one item. Why does unpacking return a tuple? >>> x = struct.pack(">i",1) >>> str(x) '\x00\x00\x00\x01' >>> y = struct.unpack(">i",x) >>>…
Jedi
  • 3,088
  • 2
  • 28
  • 47
13
votes
3 answers

How can I convert a 48 hex string to bytes using Perl?

I have a hex string (length 48 chars) that I want to convert to raw bytes with the pack function in order to put it in a Win32 vector of bytes. How I can do this with Perl?
ron
  • 281
  • 2
  • 3
  • 9
12
votes
5 answers

PHP Passing an array to pack() function

pack() syntax is (from http://php.net/manual/en/function.pack.php) string pack ( string $format [, mixed $args [, mixed $... ]] ) so assuming I need to pack three bytes $packed = pack( "c*", 65, 66, 67 ); But what if I have to pack an arbitrary…
Paolo
  • 15,233
  • 27
  • 70
  • 91
12
votes
4 answers

Ruby's pack and unpack explained

Even after reading the standard documentation, I still can't understand how Ruby's Array#pack and String#unpack exactly work. Here is the example that's causing me the most trouble: irb(main):001:0> chars = ["61","62","63"] => ["61", "62",…
Gadi A
  • 3,449
  • 8
  • 36
  • 54
11
votes
7 answers

pack() in php. Illegal hex digit warning

i am having some problems using pack() in php $currencypair = "EUR/USD"; $buy_sell = "buy"; $alert_device_token =array("a","a","b"); $message = "Your " . $currencypair . " " . $buy_sell . " alert price has been reached!"; $payload['aps'] = array ( …
ayush
  • 14,350
  • 11
  • 53
  • 100
11
votes
2 answers

Lua - packing IEEE754 single-precision floating-point numbers

I want to make a function in pure Lua that generates a fraction (23 bits), an exponent (8 bits), and a sign (1 bit) from a number, so that the number is approximately equal to math.ldexp(fraction, exponent - 127) * (sign == 1 and -1 or 1), and then…
RPFeltz
  • 1,049
  • 2
  • 12
  • 21
10
votes
3 answers

How to pack a UUID into a struct in Python?

I have a UUID that I was thinking of packing into a struct using UUID.int, which turns it into a 128-bit integer. But none of the struct format characters are large enough to store it, how to go about doing this? Sample code: s =…
nicobatu
  • 1,377
  • 3
  • 15
  • 34
10
votes
5 answers

Perl's Pack('V') function in Python?

I've been working on some exploit development recently to get ready for a training course, and I've run into a problem with a tutorial. I've been following along with all the tutorials I can find, using Python as opposed to the language the…
Schinza
  • 101
  • 1
  • 4
10
votes
3 answers

Is there anyway to "pack" a JPanel?

Is there any thing I can do to make my JPanel pack like a JFrame, or do something like that. I want to do this instead of giving it dimensions. Please comment if any addition info is needed. Thanks.
MattS
  • 113
  • 2
  • 3
  • 11
10
votes
3 answers

64-bit Float Literals PHP

I'm trying to convert a 64-bit float to a 64-bit integer (and back) in php. I need to preserve the bytes, so I'm using the pack and unpack functions. The functionality I'm looking for is basically Java's Double.doubleToLongBits() method.…
kryo
  • 716
  • 1
  • 6
  • 24
9
votes
1 answer

How to convert Hex string to IEEE754 Float number with Raku?

In Python, unpack can convert Hex string to IEEE754 Float number: import struct print(struct.unpack('
ohmycloudy
  • 629
  • 5
  • 13
9
votes
3 answers

Pack, but don't make it smaller

I have JFrame with GridBagLayout. User can resize this window. Also, he can perform some editing actions that change window size. I use pack(); repaint(); now after such actions. But, actually I shouldn't make window smaller after such operations -…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
9
votes
3 answers

Parameter Pack with alternating types

I have a struct C which gets initialized with a variable number of instances of struct A and struct B. E.g.: struct A {}; struct B {}; struct C { C(A&& o1, B&& p1, A&& o2) {} C(A&& o1, B&& p1, A&& o2, B&& p2, A&& o3) {} C(A&&…
PeWe
  • 99
  • 1
9
votes
3 answers

How to make an Ant task to sign and pack200 all my JAR files?

My JAR files must be signed for a webstart application. It would be nice to also have them packed to minimize the download time. I'm trying to configure an Ant task to automatically do it during the deploy of the application. Since the pack process…
neves
  • 33,186
  • 27
  • 159
  • 192
8
votes
3 answers

How to switch byte order of binary data

I have a binary file which I'm reading where some 2 byte values are stored in 'reverse' byte order (little endian?), eg. 1D 00 13 00 27 00 3B 00 45 00 31 00 4F The original program that created these values stores them internally as shorts. Those…
pfyon
  • 332
  • 1
  • 6
  • 10
1
2
3
45 46