Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Subtract 2 hexadecimal numbers

by sundialsvc4 (Monsignor)
on Feb 21, 2014 at 15:07 UTC ( #1075738=note: print w/ replies, xml ) Need Help??


in reply to Subtract 2 hexadecimal numbers

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.


Comment on Re: Subtract 2 hexadecimal numbers

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1075738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-09-19 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (138 votes), past polls