awk, how to convert hex to decimal

Art Neilson art at pilikia.pegasus.com
Wed Feb 6 20:23:21 AEST 1991


In article <23000 at well.sf.ca.us> gregs at well.sf.ca.us (Greg Strockbine) writes:
>How do I convert a hex number (or is it string) into decimal in awk ?

Here's my stab at it!

function htoi(s) {
	split("A\tB\tC\tD\tE\tF", u)
	len = split("a\tb\tc\td\te\tf", l)
	for (i = 1; i <= len; i++)
		uhex[u[i]] = lhex[l[i]] = 9 + i
	for (i = 1; i <= length(s); i++) {
		c = substr(s, i, 1)
		if (c ~ /[0-9]/)
			v = c + 0
		if (c ~ /[A-F]/)
			v = uhex[c]
		if (c ~ /[a-f]/)
			v = lhex[c]
		n = 16 * n + v
	}
	return n
}
-- 
Arthur W. Neilson III		| INET: art at pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pegasus!pilikia!art



More information about the Comp.unix.shell mailing list