[Top][All Lists]

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

Re: The correct way to print hex number as dec number?

From: arnold
Subject: Re: The correct way to print hex number as dec number?
Date: Sun, 15 Mar 2020 04:49:18 -0600
User-agent: Heirloom mailx 12.5 7/5/10

david kerns <address@hidden> wrote:

> hmmm, looks like my "main" machine is using "real" (as in aho weinberger

say rather, "original".

> kernighan) awk
> $ echo 0xaaaa | awk '{printf("%d\n", $1)}'
> 43690
> $ awk --version | head -1
> awk version 20070501

You're on a Mac. The Apple user land is criminally out of date.
There have been 13 years' worth of fixes since then, see the
git repo for Unix awk (https://github.com/onetrueawk/awk).

> gawk has the "bug"

It is most definitely NOT a bug.  That awk on your system accepts
hexadecimal numbers is totally an accident of the implementation: the
C library strtod routine accepts hex numbers. On a system where strtod
does not, the above code would treat 0x... as zero.

Since I strongly believe that this kind of behavior is not what
the original authors intended, and not "in the sprit of awk", gawk
does not blindly rely on strtod, but prescans data values first,
before passing the data on to be converted to a number.

I am pretty sure this is documented in the gawk manual, as well.



reply via email to

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