By printing this out in ASCII hex, you have actually changed the problem.

When you do a binmode read to a scalar, you get a $buffer where each "character" is a byte (0-255 unsigned). For actual bytes there is "nothing to be done" - it is already a byte value. For multi-byte fields some sort of unpack() is usually necessary. Use substr() to get a range of byte values. Use unpack() to convert sequences of bytes into some other representation (from little endian to big endian or whatever).

my $HeaderRecordId = substr($buf,0,1); my $FileFormatVersion = substr($buf,1,1); my $TimeStamp = substr($buf,2,8); #some kind of unpack needed here! my $NumBSC = substr($buf,10,1);
$HeaderRecordId $FileFormatVersion, $NumBSC are just bytes and nothing more is needed past substr().

Update: I looked back an some ancient code (I don't deal with binary very often), but this had to do with .WAV files. My point is that substr() will get you the sequence of bytes. Here, I look for "RIFF" and "data" with string compares. The V4 unpack is for little endian conversion.

code snippet... read(IN, my $buff, 1 * 2**10); (substr($buff,0,4) eq "RIFF") || die "not a valid RIFF file"; my $size = unpack ("V4",substr($buff,4,4)); myprint (" RIFF segment size = $size"); (substr($buff,50,4) eq "data")|| die "data segment not found"; my $dsize = unpack ("V4",substr($buff,54,4)); myprint (" DATA Segment size = $dsize");

In reply to Re: Handling Hex data with Dynamic unpack by Marshall
in thread Handling Hex data with Dynamic unpack by PerlJedi

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.