#!/usr/bin/env -S awk -f # addhex - adds hex numbers. # addhex is Copyright Daniel K. Allen, 2023. # All rights reserved. # # 12 Jul 2023 - Created by Dan Allen. (9:14:20 MDT,43.51056,112.01814) # 13 Jul 2023 - Uses hex2decimal(), and supports many numbers per line. # BEGIN { sum = n = 0 a["0"] = 0; a["1"] = 1; a["2"] = 2; a["3"] = 3; a["4"] = 4; a["5"] = 5 a["6"] = 6; a["7"] = 7; a["8"] = 8; a["9"] = 9; a["A"] = 10; a["B"] = 11 a["C"] = 12; a["D"] = 13; a["E"] = 14; a["F"] = 15; a["a"] = 10 a["b"] = 11; a["c"] = 12; a["d"] = 13; a["e"] = 14; a["f"] = 15 } { for (i = 1; i <= NF; i++) { sub(/^\$/,"",$i) # remove leading dollar signs sub(/^0x/,"",$i) # remove leading hex indicator if ($i !~ /^[0-9A-Fa-f]+$/) next # skip non-hex lines x = hex2decimal($i) printf("%8s --> %10d\n",$i,x) sum += x n++ } } END { if (n > 0) printf("%08x --> %10d SUM %.2f AVG %d N\n",sum,sum,sum/n,n) } function hex2decimal(x, d,i,n,v) # hex string to decimal integer { d = 0; n = length(x) for (i = 0; i < n; i++) { v = a[substr(x,n-i,1)] d += v * 16^i } return d }
Created: 13 Jul 2023 Modified: 14 Jul 2024