in reply to Pack or unpack dropping a nibble
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.
|
|---|