Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Reading hexadecimal numbers

Received: by 10.180.86.97 with SMTP id o1mr2058431wiz.2.1349134040790;
        Mon, 01 Oct 2012 16:27:20 -0700 (PDT)
Path: q11ni123478099wiw.1!nntp.google.com!goblin3!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From: Grant <o...@grrr.id.au>
Newsgroups: comp.lang.awk
Subject: Re: Reading hexadecimal numbers
Date: Tue, 02 Oct 2012 09:27:11 +1000
Organization: scattered bugs
Lines: 117
Message-ID: <p79k685l63346h3vjjuad85er6j28m9e3q@4ax.com>
References: <k4adsh$1ht$1@news.m-online.net> <k4aofg$jsf$1@pop.motzarella.org> <k4ap44$mvd$1@pop.motzarella.org> <k4aqdk$ghh$1@news.m-online.net> <201210011411452493@webuse.net> <k4caek$qni$1@speranza.aioe.org>
Mime-Version: 1.0
Injection-Info: mx04.eternal-september.org; posting-host="0036fd0f914dbd20b3ccd36c60000d87";
	logging-data="5088"; mail-complaints-to="ab...@eternal-september.org";	posting-account="U2FsdGVkX1+NmqdKCiu4qn/S8KA6N1n3RtXApa8zeTQ="
User-Agent: ForteAgent/7.00.32.1200
Cancel-Lock: sha1:KPLuFVOLGtKU4Laq9GRssNP9kz0=
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

On Mon, 01 Oct 2012 16:47:51 +0200, Janis Papanagnou <janis_papanag...@hotmail.com> wrote:

>Am 01.10.2012 16:11, schrieb Ed Morton:
>> Janis Papanagnou <janis_papanag...@hotmail.com> wrote:
>>
>>> On 01.10.2012 02:45, pop wrote:
>>>> pop said the following on 9/30/2012 7:34 PM:
>>>>> Janis Papanagnou said the following on 9/30/2012 4:35 PM:
>>>>>> I want to read in data that is a defined in a hexadecimal representation,
>>>>>> something like 070F (and then operate on the decimal values). In GNU awk
>>>>>> there's a special function that I can use; say,  val = strtonum("0x"$1)
>>>>>> I didn't find anything like that in standard awk, so I would have to
>>>>>> implement the conversion by hand in an own function[*]; or has anyone
>>>>>> some neat idea that I did not think of?
>>>>>>
>>>>>> Janis
>>>>>>
>>>>>> [*] Quite trivial; but if avoidable I'd prefer some built-in instead of
>>>>>> an explicit loop and a mapping table.
>>>>>>
>>>> here is another, more generic, from a magazine (I think):
>>>
>>> Thanks. I think for my purpose something simpler would suffice, say, like
>>>
>>>    function h (arg,   res, s)
>>>    {
>>>      for (s=arg; length(s); s=substr(s,2))
>>>          res = res*16 + ht[substr(s,1,1)]
>>>      return res
>>>    }
>>>
>>>    BEGIN {
>>>      n=split ("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F",t,",")
>>>      for (i=1; i<=n; i++) ht[t[i]]=i-1
>>>    }
>>>
>>>    { print $1, h($1) }
>>>
>>
>> It could be a bit simpler still:
>
>Indeed.
>
>Now let's add one more function to make it more stable WRT arguments...
>
>   function h (arg,   res, s)
>   {
>     for (s=toupper(arg); length(s); s=substr(s,2))
>         res = res*16 + index("123456789ABCDEF",substr(s,1,1))
>     return res
>   }
>
>Not bulletproof (we would have to check index()), but nice and compact.

In the context of url parameter decoding, lifted from running code:

# urldecode module
# Copyright (C) 2008 Grant Coady <gr...@bugsplatter.id.au> GPLv2
#
BEGIN {
        for (i = 0; i < 16; i++) {
                hexc[sprintf("%c", i + (i > 9 ? 55 : 48))] = i
        }
        for (i = 32; i < 127; i++) {
                ++charset[sprintf("%c", i)]
        }
}
function urldecode(s,   a, b, c, d, i)
{
        d = ""
        for (i = 1; i <= length(s); i++) {
                c = substr(s, i, 1)
                if (c == "%") {
                        a = toupper(substr(s, ++i, 1))
                        b = toupper(substr(s, ++i, 1))
                        c = sprintf("%c", hexc[a] * 16 + hexc[b])
                }
                else {
                        sub(/+/, " ", c)
                }
                d = d (c in charset ? c : " ")
        }
        return d
}
# read and decode url query string to global url_query_parm[]
function url_decode_query(   a, i)      # trashes $0 record buffer
{
        $0 = ENVIRON["QUERY_STRING"]
        gsub(/&/, " ")                  # get key=value pair fields
        for (i = 1; i <= NF; i++) {
                split($i, a, "=")       # separate key, value, then decode
                url_query_parm[a[1]] = urldecode(a[2])
        }
}
...
Written and tested with gawk 3.1.7 on slackware-11.0.

Grant.
>
>Janis
>
>>
>>    function h (arg,   res, s)
>>    {
>>      for (s=arg; length(s); s=substr(s,2))
>>          res = res*16 + index("123456789ABCDEF",substr(s,1,1))
>>      return res
>>    }
>>
>>    { print $1, h($1) }
>>
>> Regards,
>>
>>      Ed.
>>
>> Posted using www.webuse.net
>>