From: "Russ Cox" <rsc@swtch.com>
To: "Fans of the OS Plan 9 from Bell Labs" <9fans@cse.psu.edu>
Subject: Re: [9fans] awk: hex numbers? (and leak(1))
Date: Wed, 20 Sep 2006 16:20:04 -0400 [thread overview]
Message-ID: <ee9e417a0609201320xe43c72ai4f7698476710ea39@mail.gmail.com> (raw)
In-Reply-To: <82c890d00609200634u42901f17pb537f5b92a34c556@mail.gmail.com>
> 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
next prev parent reply other threads:[~2006-09-20 20:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-20 10:33 Axel Belinfante
2006-09-20 13:34 ` Gabriel Diaz
2006-09-20 13:58 ` Axel Belinfante
2006-09-20 20:20 ` Russ Cox [this message]
2006-09-20 20:29 ` Gabriel Diaz
2006-09-21 22:41 ` Axel Belinfante
2006-09-21 23:02 ` Russ Cox
2006-09-22 9:57 ` Axel Belinfante
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ee9e417a0609201320xe43c72ai4f7698476710ea39@mail.gmail.com \
--to=rsc@swtch.com \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).