Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!pacbell.com!ucsd!nosc!humu!pegasus!pilikia!art From: art@pilikia.pegasus.com (Art Neilson) Newsgroups: comp.unix.shell Subject: Re: awk, how to convert hex to decimal Summary: htoi Message-ID: <1991Feb06.092321.2262@pilikia.pegasus.com> Date: 6 Feb 91 09:23:21 GMT References: <23000@well.sf.ca.us> Distribution: usa Organization: Pilikia, Honolulu Lines: 25 In article <23000@well.sf.ca.us> gregs@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@pilikia.pegasus.com Bank of Hawaii Tech Support | UUCP: uunet!ucsd!nosc!pegasus!pilikia!art