shnatko has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: unpack() removing data
by BrowserUk (Patriarch) on Jan 10, 2013 at 22:17 UTC | |
by shnatko (Initiate) on Jan 10, 2013 at 22:55 UTC | |
by BrowserUk (Patriarch) on Jan 11, 2013 at 09:58 UTC | |
by quester (Vicar) on Jan 11, 2013 at 07:09 UTC | |
by shnatko (Initiate) on Jan 11, 2013 at 14:31 UTC | |
Re: unpack() removing data
by shnatko (Initiate) on Jan 11, 2013 at 19:51 UTC | |
by BrowserUk (Patriarch) on Jan 11, 2013 at 20:14 UTC | |
by shnatko (Initiate) on Jan 11, 2013 at 21:10 UTC |
Back to
Seekers of Perl Wisdom