[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Invalid result when converting to hex literal

From: arnold
Subject: Re: [bug-gawk] Invalid result when converting to hex literal
Date: Tue, 20 Mar 2018 03:48:18 -0600
User-agent: Heirloom mailx 12.4 7/29/08

Hi. I looked at this some more.

Steven Penny <address@hidden> wrote:

> Tested with Gawk 4.2.1 and 4.1.4. Hex literals work as expected:
>     $ awk 'BEGIN {print 0x21}'
>     33
>     $ awk --posix 'BEGIN {print 0x21}'
>     0

These are fine.

> However when converting from string to hex literal, you get the opposite
> of what is supposed to happen:
>     $ awk 'BEGIN {print +"0x21"}'
>     0

This is correct.

>     $ awk --posix 'BEGIN {print +"0x21"}'
>     33

This is also correct.


The expression +"0x21" means "take the string value given, convert it
to a number, then apply unary + to it" (effectively a no-op).  It is
absolutely the same, semantically, as something like

        { print +$1 }

The documentation describes that when working on *data*, numeric values
not in decimal are _not_ treated specially, since this goes against historical
practice and can lead to surprising results. You have to use the
--non-decimal-data option if you really want such behavior.

Thus the first case stops after the '0' and treats the numeric value
as zero.

The --posix case is different.  Here, gawk follows the standard which
says that awk calls strtod(3).  The problem is that in C99 strotod
recognizes non-decimal bases and converts them, a change from C89 and
earlier POSIX.  I've tried in the past to get this fixed in POSIX
but haven't succeeded.

for some more discussion.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]