paulrh has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to unpack type, length, value data packed as follows:
1 byte as 'type', 1 bytes as 'length', length-2 bytes of data
The length field includes the first 2 bytes, so its value is 2 greater than the length of the value.. Is there a more direct way to unpack length-2 bytes into the value? in the code below, I append 2 bytes of data to the end, read 2 extra bytes, then chop them afterwards.
my $inputString = "\x03\x04Hi\x43\x08Hello!"; my %myDict = unpack("(CC/AX2)*", $inputString . "00"); chop(%myDict); chop(%myDict); print "$myDict{0x03}\n"; # "Hi" print "$myDict{0x43}\n"; # "Hello!"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: unpack less than indicated length
by hexcoder (Curate) on Jun 03, 2018 at 18:54 UTC | |
by AnomalousMonk (Archbishop) on Jun 03, 2018 at 19:45 UTC | |
|
Re: unpack less than indicated length
by vr (Curate) on Jun 04, 2018 at 08:44 UTC | |
by Eily (Monsignor) on Jun 04, 2018 at 10:10 UTC | |
by AnomalousMonk (Archbishop) on Jun 04, 2018 at 14:36 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |