Someone just gave a good answer to a similar question
on the perl.beginners list today. It was how to detect jpg files,
but the method should work for you
#!/usr/bin/perl #all .gif, .jpg and .png file have a special header id that identify t +heir #types. for example, starting from the 7th byte of a JPEG file, you sh +ould #see the special id as: ox4A 0x46 0x49 0x46 and 0x00, the following ch +ecks #for that: my $t = undef; my($one,$two,$three,$four,$five); open(IMG,"$ARGV[0]") || die $!; sysread(IMG,$t,6); #-- discard the first 6 bytes #-- read the next five bytes sysread(IMG,$one,1); sysread(IMG,$two,1); sysread(IMG,$three,1); sysread(IMG,$four,1); sysread(IMG,$five,1); #-- unpack them and make sure they are the right magic ID #-- you can get the id from the JPEG specification if(unpack("C",$one) == 74 && unpack("C",$two) == 70 && unpack("C",$three) == 73 && unpack("C",$four) == 70 && unpack("C",$five) == 0){ print "jpeg\n"; }else{ print "not jpeg\n" } close(IMG); #you can do pretty much the same thing to png file. just go to google, + search #for png specification, find out what the maigc id should look like an +d just #code it according to the specfication.

In reply to Re: Comparing the bytes of a file by zentara
in thread Comparing the bytes of a file by Monolith-0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.