Hi. Please read this node on how to write a question that is likely to get answers. Here not only your example wasn't really short, but you explained nothing of what your program does, and how you conclude from your output that there is something wrong with what you do (or that a nibble is lost). This means that to understand the issue, you have to read and understand the code.

My advice: when in doubt, don't print, dump! (or possibly Data::Dumper with the useqq option). With use Data::Dump "pp"; and print pp($data)."\n"; you get: pack("H*","0a95e1f7e0e0f8e000e40bbf00e81791") and pack("H*","1d930a95e1f74cce00e10883fdce"). That's why I love debugging with Data::Dump it won't let an invisible char stay invisible (eg, it won't let you confuse \r\n with \n) and in this case it even detects that this is binary data and writes it as a pack call for you.

Now, your issue is here: my $binaryval = unpack("B$binarycount", pack("H$bytecount", $data));
The first mistake is that H is a nibble, so H$bytecount will display as many nibbles as their are bytes, that's only half the data.
But the main issue is that $data is binary data, and pack "H" will convert an hexadecimal string representation of binary data. (Like "1004D00" from your input). So your binary data is interpreted as a string, which is only supposed to contain 0-9 and a-f.

Now what you probably meant was: my $binaryval = unpack("B*", $data);.

Edit: too slow :). But the advice on Data::Dump still holds.


In reply to Re: Pack or unpack dropping a nibble by Eily
in thread Pack or unpack dropping a nibble by smknjoe

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.