Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hex number math.........help

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0


ok any one know a code that will do this?

take a hex number subtract 0E in hex from it convert it to dec then sub 14 then convert the answer back to hex

how would i do this?


please


thanks
 
If I was you I would do this:

$someHexNumber = "F3DA";

$number = process($someHexNumber);
print $number;

sub process {
my $original_number = shift;
my $subtract_this = hex("0E");
my $orig_num_dec = sprintf('%x', $original_number);

my $dec_val = $orig_num_dec - $subtract_this - 14;

return sprintf('%x', $dec_val);
}

I'm polishing off some Mikes Hard Lemonade, so I don't know if this works or not. Sorry - but it's my sleeping aid (tonight anyways) ;)

--jim
 
I think this might work, too.
Code:
sub process { 
    return unpack("H*",chr(hex(shift)-hex('0e')-14)) 
}
(Sometimes hex makes my head spin)
[spin2][bugeyed][flip][hammer][roll2]



jaa
 
ok question

"take a hex number" subtract 0E in hex from it convert it to dec then sub 14 then convert the answer back to hex


what happens it the first number is hex or sometimes dec? what i mean by the first number is this "take a hex number"

how would i handle something like that?


thanks


 
You could make two different subroutines, but you'd still have to know which one to call. You could make one subroutine handle both hex and dec - but then you would either have to give the sub an extra argument indicating whether the 'first number' is hex or dec OR you would have to make a sub that just 'does the right thing'. This would be just about impossible since, for instance "10" is a valid Hex number as well as a valid dec number. You COULD make a sub that assumed dec unless there was an a,b,c,d,e, or f in the value. Then you could assume it was hex.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top