Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Subtract 2 hexadecimal numbers

by dwm042 (Priest)
on Feb 21, 2014 at 20:11 UTC ( [id://1075779] : note . print w/replies, xml ) Need Help??


in reply to Subtract 2 hexadecimal numbers

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.