ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> activeperl
activeperl
Help with unpack
by Barry Brevik other posts by this author
Oct 9 2009 4:02PM messages near this date
Re: Help with DBI to get HDD size of C drive of remote PC running MS-SQL Express 2005 | RE: Help with unpack
I am writing an app which, at a certain point, needs to read a .PNG
graphics file.
 
The .PNG file is always small, so I read the whole file (binmode) into a
scalar, and this works well.
 
  if ($gfilesize = -s $gfilespec)
  {
    $GRAPHFILE = "<$gfilespec";
    if (open GRAPHFILE)
    {
      binmode GRAPHFILE;
      read GRAPHFILE, $gbuffer, $gfilesize;
      close GRAPHFILE;

The size of the file ($gfilesize) matches the size of the scalar
($gbuffer). This is good.

Then I proceed to paw through the buffer. The 1st 8 bytes of the file is
a signature, which I read and deal with. Then, the file is a series of
chunks where the 1st 4 bytes is a 32 bit number representing the size of
the chunk, and the 2nd 4 bytes is an ASCII "chunk name", such as "IHDR".

So, after the signature, the next 8 bytes look like this:
 
  00 00 00 0d 49 48 44 52    . . . . I H D R
 
I am trying to decode this with the following:
 
  $bufptr = 8;
  ($chunksize, $chunkname) = unpack "L a4", substr($gbuffer, $bufptr,
8);
 
Actually, I've tried every permutation of "unpack" template that I can
think of.

The problem is that the 2nd scalar ($chunkname) comes out fine as
"IHDR", but the first scalar comes out as "218103808" which is wrong as
you can see above, it should be "13".

I thought I was starting to understand unpack, but what am I doing
wrong??

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Barry Brevik
Brian Raven
Michael Ellery
John W Kennedy
Bill Luebkert

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved