I'm running into a strange issue with the unpack() function. I have a command which produces output in binary format which I'm trying to unpack into a hex string for parsing, printing, etc..
It appears that unpack() is removing a byte from the binary data when the hex string is created and I can't figure out why.
I put a small test script together to play around with it a bit and can work around it by writing the binary data to a file first then reading it back in but it seems quite bizarre that doing it the 2 ways would yield different results
use strict;
my $data = qx{sg_logs --page=0x34,1 pd1 -H -r > temp.bin};
$data = qx{sg_logs --page=0x34,1 pd1 -H -r };
$data = unpack("H*", $data);
open OUTFILE1, ">", "outfile1.txt" or die $!;
print "unpacked1: ".$data."\n";
print OUTFILE1 $data;
close OUTFILE1;
open FILE, "temp.bin" or die $!;
binmode FILE;
open OUTFILE2, ">", "outfile2.txt" or die $!;
while (<FILE>) {
print "unpacked2: ".unpack('H*', $_ )."\n";
print OUTFILE2 unpack('H*', $_);
}
close (FILE);
close (OUTFILE2);
When I inspect the 2 output files created, outfile1.txt is always missing a byte in the same location ( 55th byte ) vs. outfile2.txt and of course, it's a byte I'm particularly interested in when parsing the results :) Comparing the 2 file sizes also shows the 2byte difference. Any help would be greatly appreciated!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.