PiEquals3 has asked for the wisdom of the Perl Monks concerning the following question:

Help!

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
    You need to read up more on pack and unpack because "A14" implies a 14 character Ascii field so that will basically just dump the data back out. try: $seqout = unpack("H7", $seq); and see what that gets you.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Unpacking binary data with unpack()?
by mr.nick (Chaplain) on Jan 15, 2001 at 21:45 UTC
    Are you trying to get the ordinal ASCII values for the string? Meaning, 65 for "A" and 104 for "h"? If so, then try this:

    @seqout=unpack("C*",$seq); print join(",",@seqout),"\n";
Re: Unpacking binary data with unpack()?
by Rudif (Hermit) on Jan 16, 2001 at 02:41 UTC
    Hi PiEquals3
    You could take a leaf out of the XP book and proceed thus:
    1- write a test subroutine that states your intent. In test below, I assumed that you want to convert the 8 bytes in $input into their hex-ascii representation, $wanted.
    2- write some tentative conversion subroutines, like those below, and run the test on each.
    3- if desperate (none pass), post your test script to perlmonks' and see if anyone can provide a solution that passes your test.
    HTH
    Rudif
    #! perl -w use strict; sub same { shift; } sub unpacka { unpack "A*", shift; } sub unpackh { unpack "H*", shift; } sub unpackc { unpack "C*", shift; } sub test { my $code = shift; my $input = "HAL 2001"; my $wanted = "48414C2032303031"; my $testout = $code->($input); if ($testout =~ /^$wanted$/i) { print "pass\n"; } else { print "fail |$wanted|$testout|\n"; } } test \&same; test \&unpacka; test \&unpackh; test \&unpackc; __END__ fail |48414C2032303031|HAL 2001| fail |48414C2032303031|HAL 2001| pass fail |48414C2032303031|72|
Re: Unpacking binary data with unpack()?
by I0 (Priest) on Jan 15, 2001 at 23:01 UTC
    What output did you want to see?