LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2012, 03:43 PM   #1
cnelson
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Rep: Reputation: 0
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?
 
Old 06-26-2012, 03:06 AM   #2
Jebram
LQ Newbie
 
Registered: May 2007
Location: Berlin, FRG
Distribution: Ubuntu
Posts: 22

Rep: Reputation: 4
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";
}
 
Old 06-26-2012, 10:16 AM   #3
cnelson
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Original Poster
Rep: Reputation: 0
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;
}
 
Old 06-26-2012, 10:52 AM   #4
cnelson
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Original Poster
Rep: Reputation: 0
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;
}
 
Old 06-26-2012, 01:06 PM   #5
Jebram
LQ Newbie
 
Registered: May 2007
Location: Berlin, FRG
Distribution: Ubuntu
Posts: 22

Rep: Reputation: 4
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.

Last edited by Jebram; 06-26-2012 at 01:17 PM. Reason: Fixed some more typos.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl read dir script - regex gif|jpg casperdaghost Linux - Newbie 2 02-22-2012 10:47 AM
How Can I convert Hex to Binary in a perl script telecom_is_me Programming 18 10-11-2010 04:34 PM
How to read the lp job title from a Perl backend script? slinx Linux - Software 10 12-04-2008 03:45 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Apache2 CGI-Perl will not read files fefeh Linux - Software 0 06-30-2003 08:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration