unpack

PHP Pack/unpack - can it handle variable length strings

倖福魔咒の 提交于 2019-12-01 21:40:44
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 (/) associates them with a single value from the argument list. In pack, the length of the argument is

Reverting unpack('C*', “string”)

南笙酒味 提交于 2019-12-01 17:40:48
I would like to know how I can reverse what this unpack function bellow performed. I think the pack function is able to reverse what unpack performed, however I'm not sure. First I have a simple string which after unpacking it I would have an array of bytes representing such string. Now I would like to know how to reverse such array back to the original string. <?php $array = unpack('C*', "odd string"); /*Output: Array ( [1] => 111 [2] => 100 [3] => 100 [4] => 32 [5] => 115 [6] => 116 [7] => 114 [8] => 105 [9] => 110 [10] => 103 )*/ $string = pack("which format here?", $array); echo $string;

vector unpacking for octave

空扰寡人 提交于 2019-12-01 16:56:59
Octave(/matlab)'s notation for handling multiple return values [a, b] = f(x) suggests that the values returned by f(x) are in a sort of row vector and that Octave supports vector unpacking (like Python's tuple-unpacking). Yet when I put [a, b] = [1, 2] I get error: invalid number of output arguments for constant expression Does octave support vector-unpacking? If so, what's the proper notation? I can't find anything in the documentation I don't have Octave to test, but in MATLAB you can "unpack" cell arrays. x = {1 2}; [x1,x2] = x{:} x1 = 1 x2 = 2 You can convert numerical vector to a cell

Why is `{*l}` faster than `set(l)` - python sets (not really only for sets, for all sequences)

青春壹個敷衍的年華 提交于 2019-12-01 10:49:15
So here is my timings: >>> import timeit >>> timeit.timeit(lambda: set(l)) 0.7210583936611334 >>> timeit.timeit(lambda: {*l}) 0.5386332845236943 Why is that, my opinion would be equal but it's not. So unpacking is fast from this example, right? For the same reason [] is faster than list() ; the interpreter includes dedicated support for syntax based operations that uses specialized code paths, while constructor calls involve: Loading the constructor from built-in scope (requires a pair of dict lookups, one in global scope, then another in built-in scope when it fails) Requires dispatch through

Why is `{*l}` faster than `set(l)` - python sets (not really only for sets, for all sequences)

拥有回忆 提交于 2019-12-01 08:42:06
问题 So here is my timings: >>> import timeit >>> timeit.timeit(lambda: set(l)) 0.7210583936611334 >>> timeit.timeit(lambda: {*l}) 0.5386332845236943 Why is that, my opinion would be equal but it's not. So unpacking is fast from this example, right? 回答1: For the same reason [] is faster than list(); the interpreter includes dedicated support for syntax based operations that uses specialized code paths, while constructor calls involve: Loading the constructor from built-in scope (requires a pair of

struct.unpack with bytearray's

瘦欲@ 提交于 2019-12-01 00:55:25
I wrote an application that uses struct.unpack on byte arrays. Running it on my machine using python 2.7.5 it works well: >>> data bytearray(b'\x07\x00\x00\x00\x00\x00\x00\x00') >>> struct.unpack("<Q", data) (7,) however, I tried to use it with a python version 2.7.3 I got an exception: error: unpack requires a string argument of length 8 I need to explicitly cast the bytearray to string before unpacking it. Is this related to the python version change? the struct manual says nothing about this.. I would like to avoid doing all the casting, is there any way around this? As you have noticed,

When would you use unpack('h*' …) or pack('h*' …)?

南楼画角 提交于 2019-11-30 08:53:30
In Perl, pack and unpack have two templates for converting bytes to/from hex: h A hex string (low nybble first). H A hex string (high nybble first). This is best clarified with an example: use 5.010; # so I can use say my $buf = "\x12\x34\x56\x78"; say unpack('H*', $buf); # prints 12345678 say unpack('h*', $buf); # prints 21436587 As you can see, H is what people generally mean when they think about converting bytes to/from hexadecimal. So what's the purpose of h ? Larry must have thought someone might use it, or he wouldn't have bothered to include it. Can you give a real-world example where

Lua unpack bug?

北战南征 提交于 2019-11-29 09:30:55
I Have stumbled on a weird behavior in Lua unpack function table1 = {true, nil, true, false, nil, true, nil} table2 = {true, false, nil, false, nil, true, nil} a1,b1,c1,d1,e1,f1,g1 = unpack( table1 ) print ("table1:",a1,b1,c1,d1,e1,f1,g1) a2,b2,c2,d2,e2,f2,g2 = unpack( table2 ) print ("table2:",a2,b2,c2,d2,e2,f2,g2) Output: table1: true nil true false nil nil nil table2: true false nil nil nil nil nil The second unpack delivers parameters up to the first nil value. I could live with that. The first table delivers 4? parameters with one being nil in the middle. It has 4 parameters that are not

How do I unpack various form of integers in a byte buffer in Golang?

試著忘記壹切 提交于 2019-11-28 21:22:06
I need to extract various fields in a byte buffer. I came up with this solution: func (fs *FileSystem) readSB() { // fs.f is a *os.File buf := make([]byte, 1024) fs.f.ReadAt(buf, 1024) // Offset: type var p *bytes.Buffer // 0: uint32 p = bytes.NewBuffer(buf[0:]) binary.Read(p, binary.LittleEndian, &fs.sb.inodeCount) // 4: uint32 p = bytes.NewBuffer(buf[4:]) binary.Read(p, binary.LittleEndian, &fs.sb.blockCount) // 20: uint32 p = bytes.NewBuffer(buf[20:]) binary.Read(p, binary.LittleEndian, &fs.sb.firstDataBlock) // 24: uint32 p = bytes.NewBuffer(buf[24:]) binary.Read(p, binary.LittleEndian,

Python: reading 12-bit binary files

不问归期 提交于 2019-11-28 11:40:45
I am trying to read 12-bit binary files containing images (a video) using Python 3. To read a similar file but encoded in 16 bits, the following works very well: import numpy as np images = np.memmap(filename_video, dtype=np.uint16, mode='r', shape=(nb_frames, height, width)) where filename_video is the file and nb_frames, height, and width characteristics of the video that can be read from another file. By 'working very well' I mean fast: reading a 640x256 video that has 140 frames takes about 1 ms on my computer. As far as I know I cannot use this when the file is encoded in 12 bits because