LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl script to read Hex from files (https://www.linuxquestions.org/questions/programming-9/perl-script-to-read-hex-from-files-4175413319/)

cnelson 06-25-2012 03:43 PM

Perl script to read Hex from files
 
I"m trying to pull certain data out of a hex file using perl. And I am so hoping there is a shortcut or two.

What I would love to do is read certain data for example:
address: 0x00000022
Address: 0x00000024
address: 0x00000261 to 0x00000264
address: 0x00000265 to 0x00000268
address: 0x00000269 to 0x00000272

and turn those into vars that are outputted. Is there a subroutine where I can send it the address(es) and it would return the value from it?

Jebram 06-26-2012 03:06 AM

Please take a look at the output of
Quote:

perldoc -f hex
and
Quote:

perldoc -f oct
Did you want something like this?
Code:

#!/usr/bin/perl

use strict;
use warnings;

LINE:
while ( <> ) {
    m/address: \s*? ( 0? x? [\dabcdef]+ ) \s*
        (to \s* ( 0? x? [\dabcdef]+ ) )? /msxi
        or next LINE;
    my ( $var1, $var2 ) = ( oct $1, $3 );
    printf "Line $.: Adress value found: $var1 (0x%x)", $var1;
    if ( defined $var2 ) {
        $var2 = oct $var2;
        printf ", 2nd value found: $var2 (0x%x)", $var2;
    }
    print "\n";
}


cnelson 06-26-2012 10:16 AM

Thanks for the reply, I'm not sure I quite understand what you are doing for me to make sense of it.

What I tried doing was opening a file, converting to hex and using substr to pull the location, but pulling the location isn't working and I'm not sure how to make it work. I had thought this would be as easy or would have a toon of code doing what I was looking for as it is used often. And people seam to always refence hex code w/ locations values based 0x0.

This is a subset of opening file, converting to hex, then attempting to get the addresses.
Code:

$hexfile=&HEX_VIEW();

$class=&HEX_ADDY("80","2");
print "Class $class =".$Class_Table{$class}."\n";

$level=&HEX_ADDY("72","2");
print "level=$level\n";

sub HEX_VIEW{
        open(INPUT, "<$ARGV[0]") or die "ERROR : Cannot open $ARGV[0].\n";
        $value=<INPUT>;
        close(INPUT);
        $value=unpack("H*",$value);
        return $value;
}
sub HEX_ADDY{
        my($loc_start,$loc_length)=@_;
#      $loc_start=hex($loc_start);
        $new_value= substr $hexfile, $loc_start, $loc_length;
        return $new_value;
}


cnelson 06-26-2012 10:52 AM

think I have the location part working, I just find it odd that I have to do this, rather than there being an easier way to deal with hex.

Code:

sub HEX_ADDY{
        my($loc_start,$loc_length)=@_;
        $loc_start=(hex($loc_start))*2;
        $new_value= substr $hexfile, $loc_start, $loc_length;
        return $new_value;
}


Jebram 06-26-2012 01:06 PM

Quote:

Thanks for the reply, I'm not sure I quite understand what you are doing for me to make sense of it.
I tried to show You some perl code that can extract the hexadecimal values from your example data as provided in your original post. Did you run or test my code?
Quote:

What I tried doing was opening a file, converting to hex and using substr to pull the location...
That didn't make much sense for me. But from the example code You have supplied, I can infer
that you meant to extract some hex digits, as indicated by an offset value (relative to the beginning of the not-yet-encoded bytes read from the input file).
I am getting the Impression that You are learning Perl from some seriously outdated tutorials/documentation. This is hurting You!! Please consider to use the Book "Modern Perl" by chromatic, which can had for free as a PDF file. Always use strict; in your perl code (except perhaps for one-liners). Also try to get help from Perl::Critic.
Okay, back to Your code. I have tried to refactor it into something I could debug, then I have added an alternative code path and some comments explaining what I think Your codes problem is. Please read and run this, both code paths:

Code:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;  # Implicit error handling for IO.  Optional, makes this script
# simpler yet safer.

my %Class_Table;
my $file_bytes_hex_encoded;

sub read_and_convert_bytes_to_hexadecimal
{
    my $input_filename = shift;

    my $value;
    open my $input, '<', $input_filename or
        die "ERROR : Cannot open file $input_filename: $!.\n"
        ;
    {
        local $/ = undef;  # enable slurp mode to read entire file at once.
        $value = <$input>;
        # (IO Errors ignored or catched via autodie.)
    }
    close $input;
    $value = unpack( "H*", $value );
    return $value;
}

sub extract_hex_digits
{
        my( $loc_start, $substr_length ) = @_;

        printf "Initial offset into file bytes: $loc_start decimal (0x%x hex)\n",
            $loc_start
            ;
        if ( 1 ) {  # Change 1 to 0 for old bahavior.
            $loc_start = 2 * $loc_start;
            # The multiplication by 2 converts from byte offset to
            # hex digit pairs offset, which is okay...
        }
        else {
            $loc_start = 2 * hex( $loc_start );
            # ... But the "hex" invocation damages the offset!  I.e. Decimal 80
            # is interpreted as hex 0x80, which has a numeric value of
            # decimal 256!
        }

        printf "New offset into hex encoded file bytes: "
            . "$loc_start decimal (0x%x hexadecimal) - Is this intended?!?\n",
            $loc_start
            ;
        my $result = substr $file_bytes_hex_encoded, $loc_start, $substr_length;
        return $result;
}

$file_bytes_hex_encoded = read_and_convert_bytes_to_hexadecimal( $ARGV[ 0 ] );

my $class = extract_hex_digits( 80, 2 );

$Class_Table{ $class } = "I don't know what is expected here";
print "Class $class = $Class_Table{ $class }\n";

my $level = extract_hex_digits( 72, 2 );
print "level=$level\n";
exit 0;
__END__

If You don't have an input file handy, just use the script itself as input:
perl file_hex_extract.pl file_hex_extract.pl
I hope I understood Your intentions right this time.


All times are GMT -5. The time now is 02:26 AM.