pack

Converting grouped hex characters into a bitstring in Perl

六月ゝ 毕业季﹏ 提交于 2019-12-05 13:05:43
I have some 256-character strings of hexadecimal characters which represent a sequence of bit flags, and I'm trying to convert them back into a bitstring so I can manipulate them with & , | , vec and the like. The hex strings are written in integer-wide big-endian groups, such that a group of 8 bytes like "76543210" should translate to the bitstring "\x10\x32\x54\x76" , i.e. the lowest 8 bits are 00001000 . The problem is that pack 's " h " format works on one byte of input at a time, rather than 8, so the results from just using it directly won't be in the right order. At the moment I'm doing

PHP Passing an array to pack() function

流过昼夜 提交于 2019-12-05 12:29:13
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 number of bytes? They could coveniently be stored into an array so I naively tried $a = array( 65, 66, 67 ); $packed = pack( "c*", $a ); But it doesn't work. Is there a way to make pack() work with an array ? At little late to the party, but for future reference, you can use the new ... operator (v5.6+) to explode the array inline: $packed = pack("c*",

How to pack a UUID into a struct in Python?

本秂侑毒 提交于 2019-12-05 09:02:52
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 = struct.Struct('L') unique_id = uuid.uuid4() tuple = (unique_id.int) packed = s.pack(*tuple) The problem is, struct format 'L' is only 4 bytes...I need to store 16. Storing it as a 32-char string is a bit much. It is a 128-bit integer, what would you expect it to be turned into? You can split it into several components — e.g. two 64-bit integers: max_int64 =

compute CRC of struct in Python

牧云@^-^@ 提交于 2019-12-05 08:33:24
I have the following struct, from the NRPE daemon code in C: typedef struct packet_struct { int16_t packet_version; int16_t packet_type; uint32_t crc32_value; int16_t result_code; char buffer[1024]; } packet; I want to send this data format to the C daemon from Python. The CRC is calculated when crc32_value is 0 , then it is put into the struct. My Python code to do this is as follows: cmd = '_NRPE_CHECK' pkt = struct.pack('hhIh1024s', 2, 1, 0, 0, cmd) # pkt has length of 1034, as it should checksum = zlib.crc32(pkt) & 0xFFFFFFFF pkt = struct.pack('hhIh1024s', 2, 1, checksum, 0, cmd) socket

Python struct.pack() for individual elements in a list?

寵の児 提交于 2019-12-05 00:01:17
I would like to pack all the data in a list into a single buffer to send over a UDP socket. The list is relatively long, so indexing each element in the list is tedious. This is what I have so far: NumElements = len(data) buf = struct.pack('d'*NumElements,data[0],data[1],data[2],data[3],data[4]) But I would like to do something more pythonic that doesn't require I change the call if I added more elements to the list... something like: NumElements = len(data) buf = struct.pack('d'*NumElements,data) # Returns error Is there a good way of doing this?? Yes, you can use the *args calling syntax.

What is the meaning of the letters in the output from struct.pack?

大城市里の小女人 提交于 2019-12-04 21:53:50
when i change number into hex in struct module of python, >>> import struct >>> struct.pack("i",89) 'Y\x00\x00\x00' >>> struct.pack("i",890) 'z\x03\x00\x00' >>> struct.pack("i",1890) 'b\x07\x00\x00' what is the meaning of "Y,z,b" in the output? oldrinb You're not converting to hex. You're packing the integer as binary data... in this case, little-endian binary data. The first characters are just the corresponding ASCII characters to the raw bytes; e.g. 89 is Y , 122 is z , and 98 is b . The first pack produces '\x59\x00\x00\x00' for 0x00000059 ; '\x59' is 'Y' . The second produces '\x7a\x03

Perl pack/unpack and length of binary string

僤鯓⒐⒋嵵緔 提交于 2019-12-04 15:37:18
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 length($unparray[0])."\n" # Prints 3 printf "%d\n", $unparray[0] ' # Prints 255 # As a one-liner: # perl -e '$a = pack("d",255); print length($a)."\n"; $aa = pack("dd", 255,123,0,45,123); print length($aa)."\n"; @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; print length($unparray[0])."\n"; printf "%d\n", $unparray[0] ' Now, I'd expect a double

PHP Pack/unpack - can it handle variable length strings

元气小坏坏 提交于 2019-12-04 04:34:01
问题 I've been trying to figure out if the PHP implementation of Pack/Unpack can do something that the Perl version is able to do. The example I'd like to be able to do in PHP is: http://perldoc.perl.org/perlpacktut.html#String-Lengths # pack a message: ASCIIZ, ASCIIZ, length/string, byte my $msg = pack( 'Z* Z* C/A* C', $src, $dst, $sm, $prio ); # unpack ( $src, $dst, $sm, $prio ) = unpack( 'Z* Z* C/A* C', $msg ); What this Perl code does is described as: Combining two pack codes with a slash (/)

Convert a Perl code to PHP

二次信任 提交于 2019-12-02 22:52:43
问题 I need to convert the following perl function to php: pack("SSA12AC4L", $id, $loc, $name, 'ar', split(/\./, $get->getIP), time+(60*60); I use the following code (to test) in PHP: echo pack("SSA12AC4L", '25', '00001', '2u7wx6fd94fd', 'f', preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY), time()+(60*60)); But I'm getting the following error: Warning: pack() [function.pack]: Type C: too few arguments in D:\wamp\www\test.php on line 8 Any suggestions? Thanks a lot. 回答1: The problem is that

Convert a Perl code to PHP

一世执手 提交于 2019-12-02 13:09:01
I need to convert the following perl function to php: pack("SSA12AC4L", $id, $loc, $name, 'ar', split(/\./, $get->getIP), time+(60*60); I use the following code (to test) in PHP: echo pack("SSA12AC4L", '25', '00001', '2u7wx6fd94fd', 'f', preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY), time()+(60*60)); But I'm getting the following error: Warning: pack() [function.pack]: Type C: too few arguments in D:\wamp\www\test.php on line 8 Any suggestions? Thanks a lot. The problem is that the code is giving to pack() (I am referring the the last arguments) a character, an array, and an integer.