Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Subtract 2 hexadecimal numbers

by samd (Novice)
on Feb 20, 2014 at 20:34 UTC ( [id://1075644]=perlquestion: print w/replies, xml ) Need Help??

samd has asked for the wisdom of the Perl Monks concerning the following question:

consider a text file consisting of hexadecimal entries as shown below.

0x405d75=inst1 0x405d7a=inst2 0x4035f7=inst1 0x4035f8=inst2 0x4035f8=inst1 0x4035fb=inst2 0x4035fb=inst1 0x4035fc=inst2 0x4035fc=inst1 0x4035fd=inst2 0x405d7a=inst1 0x405d81=inst2 . . . . .

how do i subtract (inst2-inst1) and diplay the difference in hexadecimal and also in integer value in perl.

Thanks in advance!

Replies are listed 'Best First'.
Re: Subtract 2 hexadecimal numbers
by toolic (Bishop) on Feb 20, 2014 at 20:54 UTC
    Read Writeup Formatting Tips and edit your post using "code" tags around your text file contents. We can't tell how many lines are in your file. This should get you started:
    use warnings; use strict; while (<DATA>) { if (/(\w+)=\w+ (\w+)=/) { my $diff = hex($2) - hex($1); printf "0x%x %d\n", $diff, $diff; } } __DATA__ 0x405d75=inst1 0x405d7a=inst2

      This is the piece of code that i wrote to extract the above mentioned hexadecimal values and now i want to subtract the $valu2 with $value1 and print it in the same output file F2. I am not sure how to subtract these 2 values and print the result in hexadecimal and also in integer

      #!usr/bin/perl open (FILE1, "<file_hex.txt") or die "can't read the file"; open (FILE2, ">output_log.txt") or die "cant write into the file"; while (<FILE1>) { if($_ =~ (/(^\=\>\s+)(0x[0-9a-f]+){1,10}(.*)/)) { $value1= $2; print FILE2 $value1."=inst1 \n"; } elsif($_ =~ (/(^\s+)(0x[0-9a-f]+){1,10}(.*)/)) { $value2=$2; print FILE2 $value2."=inst2 \n"; } } close (FILE1); close (FILE2);

      Thanks

        The basic technique for extraction and substraction was shown by toolic. I don't know why you haven't used this nor why you seem to be asking exactly the same question in response to his reply: he's shown you how to extract the data, do the subtraction and output the result in the formats requested. Perhaps you should provide expected output to clarify your question.

        Now that we can see the format of your input data, I can show you how to read pairs of lines.

        #!/usr/bin/env perl use strict; use warnings; { local $/ = "inst2\n"; while (<DATA>) { /^(\w+)=\w+\s+(\w+)/; printf "%#x %1\$d\n" => hex($2) - hex $1; } } __DATA__ 0x405d75=inst1 0x405d7a=inst2 0x4035f7=inst1 0x4035f8=inst2 0x4035f8=inst1 0x4035fb=inst2

        Output:

        0x5 5 0x1 1 0x3 3

        -- Ken

Re: Subtract 2 hexadecimal numbers
by ww (Archbishop) on Feb 20, 2014 at 21:52 UTC
    or, as a simplification & TIMTOWTDI:
    perl -E "my $foo = (0x0f-0x0e); say $foo;" 1 perl -E "my $foo = (0x10f-0x0e); say $foo;" 257 perl -E "my $foo = (0x10f-0x01); say $foo;" 270

    Where the remaining conversion is left as an exercise to the author of the ill-formatted, gimmé question which I have downvoted for laziness (of a non-Perlish character) and failure to read the directions on formatting.

    This section of the Monastery is known as is "Seekers of Perl Wisdom" ...not "Spare me the need to think or to read documentation, tuts, etc." SMTNTTOTRD,T,E is a very unwieldy and ugly title... and not what this site is about.

    In addition to following toolic's fine suggestion about honoring the local markup scheme, you may wish to read On asking for help and How do I post a question effectively?.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: Subtract 2 hexadecimal numbers
by Bloodnok (Vicar) on Feb 21, 2014 at 12:10 UTC
    Further to the previous suggestions to RTFM etc., I could very well be mistaken, but this smells a little like homework to me...

    A user level that continues to overstate my experience :-))
Re: Subtract 2 hexadecimal numbers
by sundialsvc4 (Abbot) on Feb 21, 2014 at 15:07 UTC

    That would be the devil of a piece of homework ... it could be a real business requirement.   And, in any case, it ought to be comparatively simple as “an exercise for the reader / student.”

    In any case, you merely need to read the file two lines at a time ... of course, die() if there is no second-line available.   Yes, that means two read-statements ... one driving the while loop, and the second one within it.   Then in this case you can simply split() each line on the equal-sign character.   Presumably you “know” that the second half of the first line will be inst1, and of the second line, inst2, but once again I think that you should check, and die() if this is not so.   (Pragmatically speaking, only your program will be in a position to detect errors in its input file, and if there is an error in it, don’t let it go undetected and also don’t let it be blamed on you!   If your program runs to completion, it should be an affirmation that the file appears to be correct.)

    The final bit of magic is handled by the hex() function, as documented in perldoc -f hex, which will convert your hex-string into an integer.   Do this for each of the two hex-strings that you now have in your hand, and print out the difference.

Re: Subtract 2 hexadecimal numbers
by dwm042 (Priest) on Feb 21, 2014 at 20:11 UTC

    There are a lot of excellent contributions here. But I thought I could take a crack at this one

    #! /usr/bin/perl my $inst1 = ""; while(<DATA>) { chomp; if ( /inst1/ ) { my ( $hex, $etc ) = split "="; $inst1 = $hex; } elsif ( /inst2/ ) { my ( $hex, $etc ) = split "="; my $diff = ($inst1 - hex($hex)) & hex("0x0ffffff"); printf "Diff = %6x hex, and %d decimal.\n", $diff, $diff; } } __DATA__ 0x405d75=inst1 0x405d7a=inst2 0x4035f7=inst1 0x4035f8=inst2 0x4035f8=inst1 0x4035fb=inst2 0x4035fb=inst1 0x4035fc=inst2 0x4035fc=inst1 0x4035fd=inst2 0x405d7a=inst1 0x405d81=inst2

    The output is:

    C:\scripts>perl hex-and-decimal.pl Diff = bfa286 hex, and 12558982 decimal. Diff = bfca08 hex, and 12569096 decimal. Diff = bfca05 hex, and 12569093 decimal. Diff = bfca04 hex, and 12569092 decimal. Diff = bfca03 hex, and 12569091 decimal. Diff = bfa27f hex, and 12558975 decimal.

    David.

Re: Subtract 2 hexadecimal numbers
by AlbinoSleestak (Initiate) on Feb 21, 2014 at 22:17 UTC
    I found how to do this on a basic level via PATH=$PATH:/usr/perl5/bin; perloc -h hex I found the hex function within "Perl In A Nutshell" Here's my contribution:
    bash-3.00# vi 2hex "2hex" 12 lines, 191 characters #!/usr/bin/perl $deca=hex("0x175"); $decb=hex("0x1aa"); print "0x175 hex is $deca in decimal\n"; print "0x1aa hex is $decb in decimal\n"; $diff = $decb - $deca; print "the difference is $diff\n";
    ---end ---

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1075644]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2025-07-09 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.