PiEquals3 has asked for the wisdom of the Perl Monks concerning the following question:
Fair warning: Though I've found Perl to be extremely useful and not an inconsiderable amount of fun, I am still somewhat new to the language.
I'm trying to use unpack() to convert packed binary data into ASCII. I'm using the program below, but the output is identical to the input! I seem to be missing some basic theory about what packed data is..
Is the Perl unpack() function not based on the same concepts that packed data fields employ? I wish I could be more lucid, but I'm pretty much at a loss for what to do..
Here's the code: (the field I'm trying to unpack is supposed to be a sequence number on a fixed-length record.)
$description = "Unpacks packed data."; ($ARGV[1]eq"")?&usage:nop; #ensure the proper number of parameters $ifn=$ARGV[0];$ofn=$ARGV[1]; open IFN, "$ifn" or die "Can't open $ifn:$!\n"; open OFN, ">$ofn" or die "Can't open $ofn:$!\n"; print "$0:$ifn->$ofn..\n"; binmode(IFN); while(<IFN>){ ($.%1000)?nop:print "\rRecords processed:$."; $seq = substr($_,1,7); ###################################################### $seqout = unpack("A14", $seq); #<--Here's the problem ###################################################### print OFN "Input[$.]: >>$seq<<\n"; print OFN "Output[$.]: >>$seqout<<\n\n"; }; print "\rRecords processed:$.\n"; print "Done.\n"; exit; ##############NORMAL TERMINATION HERE############## sub usage{ print "$0\n\n"; print "$description\n\n"; ($fn) = ($0 =~/(.*)\.(.*)/); print "USAGE: perl $fn <inputfile> <outputfile>\n\n"; exit; };
Some sample output:
Input[1]: >> << Output[1]: >> << Input[2]: >> i? << Output[2]: >> i?<< Input[3]: >> << Output[3]: >> <<
My final question: How do I take this input and extract the information from it?
-- "The snark was a boojum, you see.."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unpacking binary data with unpack()?
by extremely (Priest) on Jan 15, 2001 at 21:04 UTC | |
|
Re: Unpacking binary data with unpack()?
by mr.nick (Chaplain) on Jan 15, 2001 at 21:45 UTC | |
|
Re: Unpacking binary data with unpack()?
by Rudif (Hermit) on Jan 16, 2001 at 02:41 UTC | |
|
Re: Unpacking binary data with unpack()?
by I0 (Priest) on Jan 15, 2001 at 23:01 UTC |