[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-chat] Re: [avr-libc-dev] [bug #32322] snprintf() Hex conversion fie
From: |
Rick Mann |
Subject: |
[avr-chat] Re: [avr-libc-dev] [bug #32322] snprintf() Hex conversion field width specifiers malfunction |
Date: |
Tue, 1 Feb 2011 16:18:41 -0800 |
On Feb 1, 2011, at 16:02:06, Frédéric Nadeau wrote:
> It is my understanding that the reported behavior is normal.
>
> using gcc 4.5.0 on OpenSUSE 11.3, the following gives the same result
> #include <stdio.h>
>
> int main(int argc, const char** argv) {
> printf("%#02x\n", 0); // Writes "00"
> printf("%#02x\n", 1); // Writes "0x1"
> printf("%#02x\n", 0xb1); // Writes "0xb1"
> return 0;
> }
> gcc -Wall main.c
> ./a.out yeld:
> 00
> 0x1
> 0xb1
>
> which is the same that you have provided in the code's comments.
> - "%#02x\n", 0 - the '#' only works for non-zero value, so it prints
> the value 0 with a leadind 0.
> As for the other two, remember than the "0x" is part of the width. If
> you actully want 0x01 use:
> %#04x\n", 1
Ah! interesting. Fair enough, regarding the field width. In the past, I've
always written "0x%02x", and only started using the "#" last night as I was
logging my parsing issues.
I actually don't need the "#" flag for my purposes. The problem I initially
encountered was that "%02x" didn't zero-pad. That might've been due to the
minimal library not doing padding, although I'd argue that's probably
reasonable functionality (more so that "#" or "*", for example) for a minimal
library.
- [avr-chat] Re: [avr-libc-dev] [bug #32322] snprintf() Hex conversion field width specifiers malfunction,
Rick Mann <=