SPUG: pack and unpack

Richard Wood wildwood_players at yahoo.com
Thu Aug 28 09:23:13 CDT 2003


David, Ben, Gary, 

Gary - picked up Third Edition of the Camel, you are
correct, lots more examples, Thanks!

David - yep, those numbers of mine looked very
suspicious.  Not sure why it was happening.  The data
came from an actual TIFF file.

Ben - Thanks for the update on your example.  Mine is
now working well with essentially what you provided.
I believe I am now on my way to success.  Thanks very
much to all.

Rich Wood

seek(TIF, $offset, 0) or die "Seek $i: $!\n";
    read(TIF, $buffer, 8);               
    ($tag, $type, $count) = unpack "nnN", $buffer;
    read(TIF, $buffer, 8);
    if ($type == 1) {         
	$value = unpack "C", $buffer;
    } elsif ($type == 2) {    
	$value = unpack "Z$count", $buffer;
    } elsif ($type == 3) {
	$value =unpack "n", $buffer;
    } elsif ($type == 4) {
	$value = unpack "N", $buffer;
    } elsif ($type == 5) {
	($dv1, $dv2) = unpack "NN", $buffer;
	$value = "$dv1:$dv2";
    }
}
#
# now go get that 61 char ASCII string at address 288
#
seek(TIF, 288, 0) or die "Seek $i: $!\n";
read(TIF, $buffer, 61);
$decoded_value = unpack "Z61", $buffer;
printf("%61s\n", $decoded_value);

RESULTS:
LONG    1            0         
LONG    1         5096         
LONG    1         3304         
SHORT    1            1         
SHORT    1            4         
SHORT    1            0         
ASCII   61          288         
SHORT    1          512         
SHORT    1            1         
SHORT    1            1         
LONG    1         3304         
LONG    1        72908         
RATIONAL    1 256:18546693         
RATIONAL    1 264:19202052         
LONG    1            0         
SHORT    1            2         

ME_P63154                        CD0002 21 24 01  V 
101  00



--- Ben Reser <ben at reser.org> wrote:
> On Wed, Aug 27, 2003 at 04:03:16PM -0700, Richard
> Wood wrote:
> > Right, 
> > 
> > Got the "Z" working on a machine with a newer
> version
> > of perl (doesn't work on was 5.004_04, does work
> on
> > 5.005_03).  I only have second edition of Camel. 
> I
> > will take a look at third edition tonight.
> > 
> > All of my LONGs are coming out 808464432 and all
> of my
> > SHORTS are coming out 12336.  I will figure this
> out
> > sooner or later. 
> > 
> > Sorry I can't send a file.  They belong to a local
> > airplane company and I am not at liberty to
> transmit
> > them.
> > 
> > elsif ($type == 3) {                       # SHORT
> >        $decoded_value =unpack "n", $value;
> >     } elsif ($type == 4) {                 # LONG
> >        $decoded_value = unpack "N", $value;
> >     }
> 
> I don't know what I was thinking when I wrote that
> sample code,
> This will work better (I hope):
> 
> seek(TIF, $offset, 0) or die "Seek $i: $!\n";       
>                            
> read(TIF, $buffer, 8);                              
>                           
> ($tag, $type, $count, $value) = unpack "nnN",
> $buffer;                       
> read(TIF, $buffer, 4);
> if ($type == 1) {                                   
>                            
>   $value = unpack "C", $buffer;                     
>                     
> } elsif ($type == 2) {                              
>                            
>   $value = unpack "Z$count", $buffer;               
>                     
> } elsif ($type == 3) {                              
>                            
>   $value =unpack "n", $buffer;                      
>                     
> } elsif ($type == 4) {                              
>                            
>   $value = unpack "N", $buffer;                     
>                     
> ...                                                 
>                            
> 
> Can't imagine who that local airplane company might
> be... :)
> 
> -- 
> Ben Reser <ben at reser.org>
> http://ben.reser.org
> 
> "What upsets me is not that you lied to me, but that
> from now on I can
> no longer believe you." -- Nietzsche
>
_____________________________________________________________
> Seattle Perl Users Group Mailing List  
> POST TO: spug-list at mail.pm.org 
> http://spugwiki.perlocity.org
> ACCOUNT CONFIG:
> http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> WEB PAGE: http://www.seattleperl.org
> 


=====
Richard O. Wood
Wildwood IT Consultants, Inc.
wildwood_players at yahoo.com
425.281.1914 mobile
206.544.9885 desk

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com



More information about the spug-list mailing list