COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### %#08x formatting of printf

• Follow

```Hi,

I use %#08x to print unsigned integers in hexadecimal format. According
to C ref. man. Harbison & Steele, #08 stands for "pad the number with
up to 8 zeroes to complete it to 8 digit number". Is this correct
understanding? However this is not always the case. I sometimes see 4,
sometimes 2, that does not complete the whole number into 8 digits.  I
don't know what changes this, but do you have an alternative that

Thanks,

```
 0
Reply bilgehan.balban (64) 1/26/2006 12:26:38 PM

```Bilgehan.Balban@gmail.com wrote:
> Hi,
>
> I use %#08x to print unsigned integers in hexadecimal format. According
> to C ref. man. Harbison & Steele, #08 stands for "pad the number with
> up to 8 zeroes to complete it to 8 digit number". Is this correct
> understanding? However this is not always the case. I sometimes see 4,
> sometimes 2, that does not complete the whole number into 8 digits.  I
> don't know what changes this, but do you have an alternative that
> definitely pads to 8 digits?
>
> Thanks,
>
The # is a flag prefixing "0x" to the result
The 8 is the minimum field width (but some of the field may consist of
spaces before the "0x"). You really need a precision of 8 and a field
width of 10 (to include the "0x", so you need: "%#10.8x"

Robert
```
 0
Reply robert.f.harris (386) 1/26/2006 12:42:47 PM

```No time to check, but as I recall I used something like

printf("%08x", ....) or printf("0x%08", ...) for the prefix. I never
used the # tag.

--

Henryk

```
 0
Reply henryk.mueller (49) 1/26/2006 2:47:46 PM

```On 2006-01-26, Henryk <henryk.mueller@gmx.de> wrote:
> No time to check, but as I recall I used something like
>
> printf("%08x", ....) or printf("0x%08", ...) for the prefix. I never
> used the # tag.

That's because the # tag doesn't apply to a value of zero.
```
 0
Reply random832 (841) 1/26/2006 4:04:37 PM

```Robert Harris wrote:
> The # is a flag prefixing "0x" to the result
> The 8 is the minimum field width (but some of the field may consist of
> spaces before the "0x"). You really need a precision of 8 and a field
> width of 10 (to include the "0x", so you need: "%#10.8x"
>
> Robert

Thanks, I'll try this.

```
 0
Reply bilgehan.balban (64) 1/26/2006 5:10:35 PM

```In article <bt3Cf.197471\$D47.66412@fe3.news.blueyonder.co.uk>
>> I use %#08x to print unsigned integers in hexadecimal format. ...
>> #08 stands for "pad the number with up to 8 zeroes to complete
>> it to 8 digit number". Is this correct understanding?

Almost.  See below ...

Robert Harris  <robert.f.harris@blueyonder.co.uk> wrote:
>Bilgehan.Balban@gmail.com wrote:
>The # is a flag prefixing "0x" to the result
>The 8 is the minimum field width (but some of the field may consist of
>spaces before the "0x"). You really need a precision of 8 and a field
>width of 10 (to include the "0x", so you need: "%#10.8x"

Both of these are correct but incomplete.

As Jordan Abel pointed out elsethread, the "#" flag ("alternate
form") only applies the prefix is the number is nonzero (for both
octal and hex conversions, in fact).  ("Alternate form" has different
meanings for floating-point formats as well.)

The "0" in "08" is also a flag, meaning "pad with zeros instead of
blanks" (for numeric conversions -- the effect is undefined for
conversions like %s).  To include a zero flag, it must (obviously)
appear before a numeric field width: "%09d" means "pad with zeros,
9 wide, decimal" but "%90d" means "pad with blanks, 90 wide,
decimal".  The zero flag only applies for right-justified fields;
if you specify both the "-" flag and the "0" flag, the "0" flag is
ignored: "%-9d" and "%-09d" mean the same thing (as does "%0-9d").

Using a precision, as in Robert Harris' example above, is roughly
equivalent to specifying the zero flag.  But it is not exactly the
same.  For instance:

printf("5.2d: >%5.2d<\n", 5);
printf("05d:  >%05d<\n", 5);

prints lines with ">   05<" and ">00005<" respectively.

Using "%#10.8x" will do the Right Thing for all nonzero numbers:

printf("*%#10.8x*\n", 0x4321); /* prints *0x00004321* */

but for 0 you get:

printf("*%#10.8x*\n", 0); /* prints *  00000000* */

Note that the 0x has disappeared (as required for "%#x" format),
so 10.8 -- a field width of 10, with a precision of 8 -- now pads
with blanks.

You could leave out the field width ("%#.8x"), but then nonzero
numbers will occupy 10 character fields (8 digits plus the leading
0x) while zero will use 8 character (8 digits, no leading 0x).
You could even write "%#010.8x" and hope for zero padding when
the 0x is omitted, but this seems ... wrong. :-)

The two "best" alternatives are likely "0x%.8x" or "0x%08x",
which will do exactly the same thing.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W)  +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.
```
 0
Reply nospam252 (1722) 1/26/2006 6:37:58 PM

5 Replies
29 Views