Are the quantities x, y, z and n known beforehand? You can do most of this with a single unpack call. Some of the values might need to be fixed up after its parsed.

Here's probably how I would go about it:

my ($x, $y, $z, $n, $len) = ... # fill in these parameters my $format = "V$x n$y n C$z ".("Z$len" x $n); my $recsize = 4*$x + 2*$y + 2 + $z + ($n*$len); open B, '<', 'binary_file.bin' or die ...; while (read(B, $buf, $recsize) == $recsize) { my @values = unpack($format, $buf); my @x = splice(@values, 0, $x); # snip off the 4-byte ints my @y = splice(@values, 0, $y); # the 2-byte ints my $bits = splice(@values, 0, 1); my @z = splice(@values, 0, $z); # the 1-byte ints # @values now contain just the null-terminated strings # manually fix up signed vs. unsigned shorts in @y: $y[1] -= 65536 if ($y[1] > 32767); # repeat for each signed valued ...process the record... }
Admittedly, fixing up the shorts is a kludge. Unfortunately, there doesn't seem to be a format for big-endian signed values, so that's why I opted for this approach. I like to use the platform-independent formats n and v for shorts as opposed to the platform-dependent formats s and S to make the code, well, platform-independent.

Update: Fixed format based on alexm's comment.


In reply to Re: yet another "reading binary data" question by pc88mxer
in thread yet another "reading binary data" question by dwalin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.