unpack

Decoding 3-byte integer in Perl

百般思念 提交于 2020-01-07 05:34:09
问题 I'm reading a binary file format that starts out with 4 constant check bytes, followed by 3 octets that indicate how long the data portion of the record will be. I can decode this as follows: read($fh, $x, 7) or do { last if eof; die "Can't read: $!"; }; my ($type, $l1, $l2, $l3) = unpack("a4 C3", $x); my $length = $l1 << 16 | $l2 << 8 | $l3; Is there a more direct way to read that 3-byte value, without intermediate variables? Something I'm missing in the pack specifications maybe? I haven't

Converting a sniffed scapy packet to bytes

与世无争的帅哥 提交于 2020-01-06 02:54:11
问题 When sniffing packets with scapy I can save them to a variable sniffed = sniff(count=1) Now I would like to see what's inside the packet by doing print sniffed or print str(sniffed) but all this gives me is something like the following: ������0� E4h@@����������� l�� which isn't quite what I need. So how can I convert a sniffed packet into human readable Binary, or an array of Bytes or something more useful so that I can see what's inside? I have already tried using struct.unpack(format,

python struct unpack

≯℡__Kan透↙ 提交于 2020-01-05 07:40:06
问题 I'm trying to convert the following perl code: unpack(.., "Z*") to python, however the lack of a "*" format modifier in struct.unpack() seems to make this impossible. Is there a way I can do this in python? P.S. The "*" modifier in perl from the perldoc - Supplying a * for the repeat count instead of a number means to use however many items are left, ... So although python has a numeric repeat count like perl, it seems to lack a * repeat count. 回答1: python's struct.unpack doesn't have the Z

Speed up python's struct.unpack

风流意气都作罢 提交于 2020-01-01 09:39:13
问题 I am trying to speed up my script. It basically reads a pcap file with Velodyne's Lidar HDL-32 information and allows me to get X, Y, Z, and Intensity values. I have profiled my script using python -m cProfile ./spTestPcapToLas.py and it is spending the most amount of time in my readDataPacket() function calls. In a small test (80 MB file) the unpacking portion takes around 56% of the execution time. I call the readDataPacket function like this ( chunk refers to the pcap file): packets = []

Unpack by bits in PHP

試著忘記壹切 提交于 2019-12-31 04:09:08
问题 I want to unpack a binary string into into an array by a weird sequence of 8-8-8-7 bits. I could easily do something like this, for a normal 8-8-8-8 sequence: $b=unpack('C*',$data); for ($i=0,$count=sizeof($b); $i < $count; $i+=4) { $out[]=array($b[$i+1],$b[$i+2],$b[$i+3],$b[$i+4]); } That would give me a 2D array of bytes, grouped by 4. But as the fourth being 7 bits, I just can't think of anything appropriate. Have you got some ideas? 回答1: Not sure if I completely understand, but if you

vector unpacking for octave

此生再无相见时 提交于 2019-12-30 18:27:52
问题 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 回答1: I don't have Octave to test, but in MATLAB you can

vector unpacking for octave

ⅰ亾dé卋堺 提交于 2019-12-30 18:27:04
问题 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 回答1: I don't have Octave to test, but in MATLAB you can

perl quick switch from quaternary to decimal

99封情书 提交于 2019-12-25 19:05:30
问题 I'm representing nucleotides A,C,G,T as 0,1,2,3, and afterwards I need to translate the sequence representing as quaternary to decimal. Is there a way to achieve this in perl? I'm not sure if pack/unpack can do this or not. 回答1: Base 4 requires exactly 2 bits, so it's easy to handle efficiently. my $uvsize = length(pack('J>', 0)) * 8; my %base4to2 = map { $_ => sprintf('%2b', $_) } 0..3; sub base4to10 { my ($s) = @_; $s =~ s/(.)/$base4to2{$1}/sg; $s = substr(("0" x $uvsize) . $s, -$uvsize);

how to unpack stxetx data in python

扶醉桌前 提交于 2019-12-25 09:17:09
问题 I'm receiving a data packet sent from a RS485 level serial bus to a ESeye Hammerkop transmitter which then URL encodes the data and sends it to my server. The request looks like the following: at=info method=GET path="/write/?type=stxetx&packet=ArcYX%01%00%00%00%00%00%00%03%00%F0%00%06%00%F0%008%0E%B0%27%00%00%E85&localpackettime=2016-12-20+16%3A00%3A27&serial=868324023356343&packettime=2016-12-20+16%3A00%3A27&receivetime=2016-12-20+16%3A00%3A28&timezone=UTC" host=dry-hollows-46605.herokuapp

struct.error: unpack requires a string argument of length 2

别说谁变了你拦得住时间么 提交于 2019-12-24 18:21:43
问题 I have this following code it works 64bit another computer but it does not work with mine and gives the fallowing error.I use Python 3.3 and the requiered libraries. I could not solve the problem please help. import matplotlib.pyplot as plt import binascii import numpy import struct array = [] out = open('output.txt','wb') a=int(input("Enter the first value:")) b=int(input("Enter the second value:")) with open("thefile.bin", "rb") as f: i=0 for i in range(0, a): byte = f.read(1) i=0 for i in