Brett Walker
2018-Feb-08 11:01 UTC
[syslinux] [PATCH] syslinux/com32: Fix the printing of left zero padded hexadecimals with a leading '0x'.
From: Brett Walker <brett.walker at geometry.com.au> When printing hexadecimal numbers to a fixed width, padded with leading zeros, and also having a leading '0x'; the resultant string can be shortened by up to two characters if any leading zero padding character required is. int hexnum = 0x00001234; printf("%08X", hexnum); // results in 00001234 printf("%#08X", hexnum); // results in 0x001234 To correct this, the zero padding counting logic needs to use ndigits instead of nchars. Signed-off-by: Brett Walker <brett.walker at geometry.com.au> --- --- ./syslinux/com32/lib/vsnprintf.c.orig +++ ./syslinux/com32/lib/vsnprintf.c @@ -126,7 +126,7 @@ format_int(char *q, size_t n, uintmax_t /* Emit zero padding */ if ((flags & (FL_MINUS | FL_ZERO)) == FL_ZERO && width > ndigits) { - while (width > nchars) { + while (width > ndigits) { EMIT('0'); width--; }
Martin Str|mberg
2018-Feb-08 15:19 UTC
[syslinux] [PATCH] syslinux/com32: Fix the printing of left zero padded hexadecimals with a leading '0x'.
On Thu, Feb 08, 2018 at 11:01:21AM +0000, Brett Walker via Syslinux wrote:> From: Brett Walker <brett.walker at geometry.com.au> > > When printing hexadecimal numbers to a fixed width, padded with leading zeros, > and also having a leading '0x'; the resultant string can be shortened by up to > two characters if any leading zero padding character required is. > > int hexnum = 0x00001234; > > printf("%08X", hexnum); // results in 00001234 > printf("%#08X", hexnum); // results in 0x001234 > > To correct this, the zero padding counting logic needs to use ndigits instead of > nchars.No. You asked for at least 8 chars output. You got 8 chars output. If you want/expect at least 10 chars output, ask for 10 chars. -- MartinS
Brett Walker
2018-Feb-08 21:08 UTC
[syslinux] [PATCH] syslinux/com32: Fix the printing of left zero padded hexadecimals with a leading '0x'.
You are right Martin. I haven't a lot of experience with C/C++. What I find somewhat confusing is if hexnum is 0xFFFF1234 I would get 10 characters. I now have a better understanding of printf's operation; the width specifier is a minimum width specifier. Thanks for pointing out the noobie mistake. The patch is bad. Thanks, Brett ________________________________ From: Syslinux <syslinux-bounces at zytor.com> on behalf of Martin Str|mberg via Syslinux <syslinux at zytor.com> Sent: Friday, 9 February 2018 2:19 AM To: Brett Walker via Syslinux Subject: Re: [syslinux] [PATCH] syslinux/com32: Fix the printing of left zero padded hexadecimals with a leading '0x'. On Thu, Feb 08, 2018 at 11:01:21AM +0000, Brett Walker via Syslinux wrote:> From: Brett Walker <brett.walker at geometry.com.au> > > When printing hexadecimal numbers to a fixed width, padded with leading zeros, > and also having a leading '0x'; the resultant string can be shortened by up to > two characters if any leading zero padding character required is. > > int hexnum = 0x00001234; > > printf("%08X", hexnum); // results in 00001234 > printf("%#08X", hexnum); // results in 0x001234 > > To correct this, the zero padding counting logic needs to use ndigits instead of > nchars.No. You asked for at least 8 chars output. You got 8 chars output. If you want/expect at least 10 chars output, ask for 10 chars. -- MartinS _______________________________________________ Syslinux mailing list Submissions to Syslinux at zytor.com Unsubscribe or set options at: http://www.zytor.com/mailman/listinfo/syslinux
Reasonably Related Threads
- [PATCH] syslinux/com32: Fix the printing of left zero padded hexadecimals with a leading '0x'.
- How to speed up int2bin conversion?
- Re: ichar() function in R : 1st implementation, RFC
- Convert decimal to binary data
- Inexplicable ASAN report. Code generation bug?