Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Handling Binary data (.gif file example) in Perl
Perl is very good for handling binary data - it can do things you can't do with other utilities and scripting languages, and things that are very much harder to do in C - that's because C's strings are null terminated and in the case on binary strings, there may be an embedded null anywhere.

Finding good examples is a bit tricky. And that's because binary data tends to come with long and involved specifications. However, a .gif image file has the height and width of the image encoded into the 7th to 10th bytes of the file, so that does make a reasonable example

# Find all files ending in ".gif" in current directory
@files = glob("*.gif");
print ("@files\n");
 
# Handle each of them in turn
foreach $picture(@files) {
        # If we can't open a file, PANIC!
        open (FH,$picture) or die ("couldn't open $picture\n");
 
        # Read first 10 bytes into $stuff
        read (FH,$stuff,10) ;
 
# Use "v" as the most significant byte is last - little endian
# Not "n" which is the other way round - big endian
        # skip 6 bytes, the pick up 2 x 2-byte integers
        # (see the manual for unpack - v means 2 byte integer ;-) )
        ($wide,$high) = unpack("x6vv",$stuff);
 
        # With a .gif file, these two numbers are the image size!
        print "$picture - $wide x $high\n\n";
 
        }


I've put plenty of comments into that code ... good practice ... and so there's not much need for extra detailed description here. But I should add that there's a pack function that's the opposite of unpack if you want to reform binary data, and you can output binary data using the regular print function - nothing special at all is needed.

Yes - it's Perl, yes the code is that short, and when you know the language really well you can write it REALLY quickly!
(written 2008-01-17 21:36:06)

 
Associated topics are indexed under
P212 - Perl - More on Character Strings

Back to
Extracting information from a file of records
Previous and next
or
Horse's mouth home
Forward to
Buses from Well House Manor, Melksham, to Bath

Some other Articles
Autovivification - the magic appearance of variables in Perl
Perl, PHP or Python? No - Perl AND PHP AND Python!
Summer Ball at Bowood - Saturday 12th July 2008
Buses from Well House Manor, Melksham, to Bath
Handling Binary data (.gif file example) in Perl
Extracting information from a file of records
How not to write Perl?
Comments on proposed Asda Superstore for Melksham
Ongoing Image Copyright Issues, PHP and MySQL solutions
Script to present commonly used images - PHP
1710 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho