nani.nareshbabu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I am new to Perl and I have one real-time problem, could someone help me. For one of my testing project I need to read through hex file, and at a particular location in the file have to know the hex value present, and compare it with the value expected and print the result and the actual value present there. If the expected value is not present, go for comparison operation and look for value +/- 10 to the expected value. Please find the basic Algorithm for this problem below. Take Hex file as an input. Read particular location in the file. Print the value present in that location. Compare the value present in the location with the expected value. If both are marching print the pass result. If value are not marching go range operation and find out the value is within +/- 10 rage of expected. If present print pass and print the value. Jump to other location in the file and perform the same operation.

Replies are listed 'Best First'.
Re: Reading Hex file data
by marto (Cardinal) on Aug 20, 2010 at 07:05 UTC

    Welcome to the Monastery. This looks like a specification for a program, which part are you stuck on, and what have you tried?

    The following links are useful if you are learning Perl:

Re: Reading Hex file data
by cdarke (Prior) on Aug 20, 2010 at 08:14 UTC
    What do you mean by a hex file? Do you mean a binary file? If that is the case then look at open and binmode.

    Assuming we are dealing with binary data here, not text, you will have to convert the binary into Perl format using unpack. You will need the exact format of the data (this is sometimes the hardest part).

    To jump to another location in the file you will need seek.
      you will have to convert the binary into Perl format

      Are you talking about fixed-width data manipulation or are you talking about the use of pack and unpack to convert between data types?, While the OP hasn't provided any sample input or output we're left to assume things but I am confused by what you meant when you said "Perl format" ....


      Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
        Perl's representation of a number (scalar) rather than the machine representation (which Perl can't manipulate).
Re: Reading Hex file data
by roboticus (Chancellor) on Aug 20, 2010 at 14:37 UTC

    nani.nareshbabu:

    OK, you have a specification. Now you just need to convert it to code and make it work.

    1. Take Hex file as an input.
      So you want a file. That's going to mean you'll need to figure out the name of the file, and you'll have to open it (in read mode, based on your next step). So read perldoc -f open for information on how to open a file. I don't know how you want to determine the filename, so you'll have to do that, yourself. (You can hardcode the name into a variable, read it from an environment variable, the command line, another file, etc.)
    2. Read particular location in the file.
      Since you're specifying "a hex file", I'm assuming that you mean a binary data file. So you'll also want to read perldoc -f binmode to make sure that the file isn't treated as a text file, perldoc -f seek and perldoc -f read to figure out how to read the location.
    3. Print the value present in that location.
      I assume that you have a handle on this much, at least.
    4. Compare the value present in the location with the expected value. If both are marching print the pass result.
      I assume you don't have a problem with this step.
    5. If value are not marching go range operation and find out the value is within +/- 10 rage of expected. If present print pass and print the value.
      This is also a pretty trivial step, so again I'll assume you can do this on your own.
    6. Jump to other location in the file and perform the same operation.
      OK, "perform the same operation" generally means that the same operation should be a subroutine. You'll need to tell the subroutine some basic information to do its job. Since it's doing something nearly the same, you need to figure out what the difference is, and figure out how to tell the subroutine what the differences are. You'll also need to figure out what the subroutine is going to do: Will it just pass back a result to the caller? Will it do a task and return nothing? Or with it do a combination of the two?
      Normally, you give information to a subroutine by passing the information as arguments. I'd suggest passing the open file handle to the subroutine, along with other information you want. So read perlsub for information on how to create subroutines and pass parameters.

    Since your specification is reasonable, you should be able to turn each of the steps into the appropriate code. Give it a whirl, and if you have any trouble, reply to this post with the code you've tried, and let us know what problem you had. If there's anything I glossed over too much, feel free to request some clarifications...

    Note also that I've ignored error checking entirely. So when you read up on the various functions, be aware that they can fail. Then write code to detect and appropriately report any failures, so the rest of your program doesn't assume that all went well and cause problems later on in your program.

    ...roboticus

Re: Reading Hex file data
by zentara (Cardinal) on Aug 20, 2010 at 14:52 UTC
    One simple example can be worth a thousand words.
    #!/usr/bin/perl $text_a = "\xca\xc0\xbd\xe7\xc3\xb3\xd2\xd7\xd7\xe9\xd6\xaf\x0a"; print $text_a; $text_b = "cac0bde7c3b3d2d7d7e9d6af0a"; print $text_b,"\n"; $text_c = pack "H*", $text_b; print $text_c; # and to convert back: $text_d = unpack "H*", $text_c; print $text_d,"\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku