bug-gawk
[Top][All Lists]
Advanced

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

Re: printf doesn't zero-pad strings


From: Wolfgang Laun
Subject: Re: printf doesn't zero-pad strings
Date: Fri, 11 Aug 2023 12:28:04 +0200

Trying to follow your suggestion of an "error/warning" you'll run into the
problem of the C standard defining such situations as being "undefined".
Which means that any implementer of this standard is free to do as they
please. This choice has been made, and that's it. The whims of the
underlying engine (i.e., the C engine) are the given, and you cannot escape
this quandary except by laborious code trying to catch the bad apple before
it is passed down to make cider.

As for printing integers in hex with zero padding, "%08x" or similar should
work perfectly well. If you know that something in your input is a
hexadecimal number, use strtonum() to make it an integer, and then you'll
get the padding you desire. (Would this be a "complex workaround"?)

Cheers
Wolfgang



On Fri, 11 Aug 2023 at 11:32, tink <tink.lq@gmail.com> wrote:

>
> On Fri, 11 Aug 2023 at 20:07, Wolfgang Laun <wolfgang.laun@gmail.com>
> wrote:
>
>> Both the gawk User Manual and "man -s 3 printf" document that the
>> modifier "0" only applies to numeric formats, which   "%s" most certainly
>> is not. This is a feature simply taken over from C and implemented using
>> its library.
>> -W
>>
> Assuming that the gawk maintainers do indeed know better than the other
> folk out there - should it then not issue an error/warning rather than just
> padding with spaces instead of zeros?
>
> In terms of "certainly not" - you're quite right. But it would still be
> nice to be able to pad the hex digits 'a'-'f' with a zero without complex
> work-arounds when they occur in input one needs to process.
>
> --
> Cheers,
> Tink
>
>


-- 
Wolfgang Laun


reply via email to

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