Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a way to read a chunk of bytes from a file and compare it to an array of byte values. I believe my problem is in reading the bytes from the file. I need to somehow get the byte values into an array, but I only know how to read strings (using the read command). Once I get that, I figured I could just use a loop to check each of the values.

On more of a side note, in my original array of values, I hardcoded some of the elements as 'a'. Will this give me the ascii value for that caracter as I expect, or does it not work that way in perl? (I am more used to C++)

Thanks in advance for any help or hints.

- Monolith

Replies are listed 'Best First'.
Re: Comparing the bytes of a file
by BrowserUk (Patriarch) on Aug 28, 2002 at 17:48 UTC

    Once you have read your string, you could then use @bytesFromFile = unpack'C*',$string; to allow you to compare them byte by byte.

    Alternatively, you could $stringFromBytes = pack'C*', @myValues; and the use any of perl's usual string comparison methods.

    Without seeing your code that's failing, or a better description of your application, it's hard to see how to help you further.


    What's this about a "crooked mitre"? I'm good at woodwork!

      Ah, pack looks like what I need to use. That should allow me to get the string that I want with all the odd bytes in it. If that doesn't work, I can still try it the other way around using unpack. Thanks for your help!

      - Monolith

Re: Comparing the bytes of a file
by sauoq (Abbot) on Aug 28, 2002 at 18:26 UTC
    I need to somehow get the byte values into an array, but I only know how to read strings

    Strings are strings of bytes. It might help you to sometimes think of them as strings of characters (particularly when you are dealing with multibyte characters) but, when it comes right down to it, when you read a string you are reading bytes.

    I hardcoded some of the elements as 'a'. Will this give me the ascii value for that caracter as I expect, or does it not work that way in perl? (I am more used to C++)

    Yes. An 'a' is an 'a' is an 'a'. What you might be missing is that perl doesn't really have the notion of ints like C or C++. A number in perl is really a string. If you want to print the ascii value for 'a' in perl, printf("%d\n", 'a'); won't work like you expect. You'll want to use ord() or unpack() to do it. So, print ord('a'), "\n" would do what you want.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Comparing the bytes of a file
by zentara (Cardinal) on Aug 29, 2002 at 17:10 UTC
     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.
Re: Comparing the bytes of a file
by valdez (Monsignor) on Aug 28, 2002 at 17:25 UTC

    The solution is simple, read command accepts the following parameters: FILEHANDLE,SCALAR,LENGTH,OFFSET. You can also use sysread, but you can read about these commands using perldoc read and perldoc sysread.

    In my little experience, it can be very helpful to read man pages even if you don't need; it is a nice way to learn new things about Perl :)

    Ciao, Valerio

    Update: sorry, I misunderstood the question.