[UCLA-LUG] hex editor

Justin justin@cs.ucla.edu
Thu, 13 Jul 2000 01:07:02 -0700


On Tue, Jul 11, 2000 at 09:50:59PM -0700, Eric Hu wrote:
>I want to replace the hex string such as 6fffff with 90. The ghex seems to 
Don't have any ideas other than write your own, but it seems to me that 6fffff
in decimal notation would be a lot bigger than 90.  Something close to 7340031.
But that's just my (Handspring) Visor talking so...  It should only be a few
lines of code anyway.  Grab off the last character, do an adjustment for a-f
(change to 10-15) and multiply by 16^n (where n is the number of characters
from the end).  Well here's a trashy looking perl script that might be what
you're looking for:


#!/usr/bin/perl -w

$\ = "\n";
$hex = shift;
$dec = 0;
$length = length($hex);
$multiplier = 1;
for($i=0; $i < $length; $i++)
{
	$char = substr($hex,$length-$i-1,1);
	if($char =~ /[a-f]/)
	{
		if($char eq "a") { $char = 10 }
		elsif($char eq "b") { $char = 11 }
		elsif($char eq "c") { $char = 12 }
		elsif($char eq "d") { $char = 13 }
		elsif($char eq "e") { $char = 14 }
		elsif($char eq "f") { $char = 15 }
		else { die "Invalid character $char in $hex\n" }
	}
	$dec += ($char * $multiplier);
	$multiplier *= 16;
}
print $dec;


There's probably something better out there, but whatever.  If this isn't what
you were talking about then good luck.
-justin