you can do it like this:
(a) first splitting the input string into complete records with (xCXX /A)*, (this skips the type, reads the length byte, goes back 2 bytes, and uses the length to read the record)
(b) then for each record extract type and string map { unpack "CxA*", $_ } (this reads the type, skips the length byte, and reads the string of the record).
use strict; use warnings; my $inputString = "\x03\x04Hi\x43\x08Hello!"; my %myDict = map { unpack "CxA*", $_ } unpack "(xCXX /A)*", $inputStri +ng; print "$myDict{0x03}\n"; # "Hi" print "$myDict{0x43}\n"; # "Hello!"
[edit]
better wording: chopping -> splitting
Your code created an extra entry in the hash (type = 0x30, empty string) because of the appended "00".
[edit2]
Incorporated the hint from AnomalousMonk (thanks!)
In reply to Re: unpack less than indicated length
by hexcoder
in thread unpack less than indicated length
by paulrh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |