From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <82c890d00609201329u640b15f9xc6782c1c216528c7@mail.gmail.com> Date: Wed, 20 Sep 2006 22:29:15 +0200 From: "Gabriel Diaz" To: "Fans of the OS Plan 9 from Bell Labs" <9fans@cse.psu.edu> Subject: Re: [9fans] awk: hex numbers? (and leak(1)) In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200609201034.k8KAY0w20632@zamenhof.cs.utwente.nl> <82c890d00609200634u42901f17pb537f5b92a34c556@mail.gmail.com> Topicbox-Message-UUID: bc54367a-ead1-11e9-9d60-3106f5b1d025 hello sorry i mixed with bc syntax. function is the right keyword. gabi On 9/20/06, Russ Cox wrote: > > define todec(str) { > > hstr="1234567890ABDCEF"; > > res=0; > > n=split(str,digit,""); > > > > for(i=1;i<=n;i++) { > > num=index(hstr,digit[i])-1; > > res=res+(num*16^(n-i)); > > } > > > > return res; > > } > > Here's an alternate version. > Among other things my awk doesn't seem to know about "define". > > BEGIN { > for(i=0; i<16; i++) > _unhex[sprintf("%x", i)] = _unhex[sprintf("%X", i)] = i > } > > function unhex(s, i, v) { > v = 0 > for (i=1; i<=length(s); i++) > v = v*16 + _unhex[substr(s,i,1)] > return v > } > > {print unhex($1)} > > Russ >