bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: gawk always prints 0 for a hexadecimal number printed using printf %


From: Aharon Robbins
Subject: Re: gawk always prints 0 for a hexadecimal number printed using printf %d
Date: Fri, 07 Nov 2008 09:48:18 +0200

Mawk "works" because it relies on the underlying C library, who's atof
converts hex values.  The awk language itself does not support conversion
of hexadecimal values in data, instead parsing the leading 0 and then
stopping at the 'x'.

Gawk, *as an extension*, understands hexadecimal *constant* values in
the source code, but that usage is not portable:

        $ mawk 'BEGIN { print 0xff }'
        0
        $ gawk --lint 'BEGIN { print 0xff }'
        gawk: warning: numeric constant `0xff' treated as hexadecimal
        255
        $ gawk --posix 'BEGIN { print 0xff }'
        0

You are best off avoiding hex values in your source code and in your data.
If you really need them, use gawk's strtonum() function and see the gawk
manual's version of strtonum in awk which can be used for portability.

HTH,

Arnold

In article <address@hidden> you write:
>Thanks John for your clarification.
>Is it possible to consider this as an enhancement request,
>if it fits the overall philosophy and goals of awk?
>As seen in my original mail, the awk version 1.2 present on
>Ubuntu Linux and written by Mike Brennan (brennan at whidbey dot com)
>supports this.
>
>Thanks,
>Mahendra Ladhe
>
>-----Original Message-----
>From: John Cowan [mailto:address@hidden 
>Sent: Friday, November 07, 2008 8:38 AM
>To: Mahendra Ladhe
>Cc: address@hidden
>Subject: Re: gawk always prints 0 for a hexadecimal number printed using
>printf %d
>
>Mahendra Ladhe scripsit:
>
>> when a field on a line which is a hexadecimal number(preceded by 0x)
>> is printed using printf with %d format specifier, gawk always prints
>> 0 instead of the value of the field in the decimal format.
>
>That is because awk's string-to-number conversion (unlike the Awk
>language
>itself) does not understand "0x..." strings, so it converts just the
>initial numeric part, which is "0".
>
>-- 
>Principles.  You can't say A is         John Cowan <address@hidden>
>made of B or vice versa.  All mass      http://www.ccil.org/~cowan
>is interaction.  --Richard Feynman
>
>


-- 
Aharon (Arnold) Robbins                                 arnold AT skeeve DOT com
P.O. Box 354            Home Phone: +972  8 979-0381
Nof Ayalon              Cell Phone: +972 50  729-7545
D.N. Shimshon 99785     ISRAEL




reply via email to

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