in reply to pack with count in the data

If not for the requirement that the length is sometimes (the OP implies randomly) given as the negative of the true length, this would be simple and probably fairly quick. As ikegami wrote, the trick is to first convert the hex input string to raw binary.

This code works and will probably be somewhat faster than the code in the OP, but will certainly be slower than ikegami's suggestion, could it be used.

C:\@Work\Perl\monks\714341>perl -wMstrict -le "print qq(\noutput:); for (@ARGV) { my $data = pack( 'H*', $_); my $offset = 0; while ($offset < length $data) { my ($kategory, $len) = unpack(qq{x$offset n n}, $data); $len = 65536 - $len if $len > 32767; my $contents = unpack(qq(x$offset x4 A$len), $data); $offset += 4 + $len; printf(qq(%04X, `%s' \n), $kategory, $contents); } print '------'; } " 0001fffd206162 0002000441424344 0001fffd20616200020004414243440005fffc454443 output: 0001, ` ab' ------ 0002, `ABCD' ------ 0001, ` ab' 0002, `ABCD' 0005, `EDC' ------
You might try "\@$offset" in place of the "x$offset"; it might give you a slight speed improvement, but I doubt it. (The backslash escape before the '@' is needed to prevent interpolation as an array in the double-quoted string of the unpack template.)