in reply to Reading hex data

Taking the simplest interpretation of your problem description and modifying the sample data somewhat, the following may be what you want:

use warnings; use strict; my $start = '0020'; my $end = '0204'; my $data = join "", <DATA>; pos ($data) = 0; while (pos ($data) < length ($data)) { last if $data !~ /\G.*?($start)/gis; my $begin = pos ($data); last if $data !~ /\G.*?($end)/gis; my $end = pos ($data) - length ($end); print ((substr $data, $begin, $end - $begin) . "\n"); } __DATA__ 045a010000020000004c4553 4f4e4452412043414c4c4544 204241434b2c20492041 +4456 00020400 495345442054484154205745 prints: 000004c4553 4f4e4452412043414c4c4544 204241434b2c204920414456 00
Update:Using index per Zaxo's suggestion would probably be fast and cleaner than this.

Perl is Huffman encoded by design.