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
4
votes
1 answer

Structure size and memory layout depending on #pragma pack

Consider the following program compiled in VC++ 2010: #pragma pack(push, 1) // 1, 2, 4, 8 struct str_test { unsigned int n; unsigned short s; unsigned char b[4]; }; #pragma pack(pop) int main() { str_test str; str.n =…
Alex F
  • 42,307
  • 41
  • 144
  • 212
4
votes
3 answers

PHP Convert int to HEX

How can I get a similar function with pack/unpack (or other short function)? function getHEX($number) { switch($number) { case 0: $ret = "\x00\x00\x00\x00"; break; case 1: $ret = "\x00\x00\x00\x01"; break; case 2: $ret =…
wandam
  • 162
  • 1
  • 2
  • 11
4
votes
2 answers

Python 3 struct.pack() printing weird characters

I am testing struct module because I would like to send simple commands with parameters in bytes (char) and unsigned int to another application. However I found some weird things when converting to little endian unsigned int, these examples print…
MithPaul
  • 129
  • 4
  • 14
4
votes
2 answers

Is there an equivalent in C# to pack and unpack from Perl?

Is there some library who permit to do the same as pack and unpack Perl function? With a template list as http://perldoc.perl.org/functions/pack.html, pack is for converting a list into a binary representation unpack is for converting binary…
Christophe Debove
  • 6,088
  • 20
  • 73
  • 124
4
votes
2 answers

ruby pack and hex values

A nibble is four bits. That means there are 16 (2^4) possible values. That means a nibble corresponds to a single hex digit, since hex is base 16. A byte is 2^8, which therefore can be represented by 2 hex digits, and consequently 2 nibbles. So here…
JohnMerlino
  • 3,900
  • 4
  • 57
  • 89
4
votes
2 answers

Does svnadmin pack work on non-upgraded 1.6 repos?

I've been attempting to run svnadmin pack on repos that were originally created with SVN 1.6. However, I see no output to suggest the command is doing anything. Is it the case that shard packing occurs automatically for native 1.6 repos (as opposed…
David Corley
  • 710
  • 5
  • 17
4
votes
1 answer

PHP to python pack('H')

I'm translating an authentication library, written in PHP to Python. It's all legacy code, the original devs long gone. They used PHP's 'pack' command to transform a string into hex using the 'H' flag. PHP's documentation describes this as 'Hex…
stakolee
  • 893
  • 1
  • 7
  • 20
4
votes
0 answers

Encrypting and decrypting android expansion file

I have successfully uploaded and published my android app + apk expansion file (with format .obb but is actually a .zip) on Google Play. I would like to set up some protection to it. Are there any ways to do it to acheieve this without using…
4
votes
3 answers

PHP: extracting packed hexadecimal numbers from a string

I'm trying to extract packed hexadecimal numbers from a string. My application is communicating with a server which sends a string with a header followed by 2 byte packed hexadecimal numbers. There are thousands of numbers in this string. What I…
Gregory Peck
  • 636
  • 7
  • 22
4
votes
3 answers

Algorithm to calculate the positions of random circles so they don't overlap

I have the following problem. I have a large region populated with a random number of circles of different sizes. If a new circle of random radius is inserted in a random location, I'd like to find a nearby position for it so it doesn't overlap with…
cangrejo
  • 2,189
  • 3
  • 24
  • 31
3
votes
1 answer

Perl pack/unpack and length of binary string

Consider this short example: $a = pack("d",255); print length($a)."\n"; # Prints 8 $aa = pack("ddddd", 255,123,0,45,123); print length($aa)."\n"; # Prints 40 @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; # Prints 5 print…
sdaau
  • 36,975
  • 46
  • 198
  • 278
3
votes
1 answer

PHP pack/unpack error

I have to convert an old "encrypted" data to a proper encryption algorithm from an old system. I have this code: function unpackString($s,$l){ $tmp=unpack('c'.$l,$s); $return=NULL; foreach($tmp as $v){ if($v>0){ …
Tech4Wilco
  • 6,740
  • 5
  • 46
  • 81
3
votes
1 answer

Restore pack value from hex string

If I have a hex value produced with e.g. my $hex = sprintf "%v02X", $packed_output and the $packed_output is the result of pack over a series of numbers i.e. my $packed_output = pack "L>*", map { $_->[0] << 16 | $_->[1] } @array; is there a way…
Jim
  • 3,845
  • 3
  • 22
  • 47
3
votes
1 answer

unpacking 4 bytes into a float in PHP

I'm stuck on how to reverse a process. This part works: $ar = unpack("C*", pack("f", -4)); array:4 [▼ 1 => 0 2 => 0 3 => 128 4 => 192 ] # in hex array:4 [▼ 1 => "00" 2 => "00" 3 => "80" 4 => "c0" ] Now I want to do the reverse:…
Tac Tacelosky
  • 3,165
  • 3
  • 27
  • 28
3
votes
1 answer

Java equivalent for PHP's pack function

I have a sample application which generates a SHA1 hash in PHP as follows. base64_encode(pack('H*', sha1($pass))); I tried to achieve the same in Java, but so far, the output is different. The approach I used is as follows (Base64 and Hex classes…
Yohan Liyanage
  • 6,840
  • 3
  • 46
  • 63